【问题标题】:Single Role Spring Security Implementation单一角色 Spring 安全实现
【发布时间】:2018-11-12 21:08:12
【问题描述】:

我一直在从事 Spring Boot 迷你项目,但我被困在这部分。

所以我们的项目可能包含许多用户,但每个用户只能有一个角色

共有三个角色:

  • 实习生
  • 引导者
  • 管理员

包括 ERD:

我的问题是我可以根据我的 ERD 实现 Spring 安全注册和授权,因为从我在互联网上的教程中看到的用户和角色实体之间存在多对多关系,或者我的 ERD 错误?

希望有人能启发我。

【问题讨论】:

    标签: spring-boot spring-security


    【解决方案1】:

    是的,你可以。

    1. 创建您自己的Authentication 实现
    2. 接口定义了方法getAuthorities()。它返回一个Collection,但如何实现它取决于您。
    3. 确保您的登录过滤器(或您可能拥有的任何身份验证机制)返回您的自定义 AuthenticatedUser(而不是您可能在示例中找到的 UsernamePasswordAuthenticationToken

    例子:

    public class AuthenticatedUser implements Authentication{
        private User user;
        public AuthenticatedUser(User user){
            this.user = user;
        }
        @Override
        public Collection<? extends GrantedAuthority> getAuthorities() {
            return Arrays.asList(new SimpleGrantedAuthority(user.getRole()));
        } 
        // rest of the code omitted
    }
    

    我不确定我是否理解您对注册的提及 - 无论如何,用户注册通常是自定义实现;你不应该有任何问题。

    【讨论】:

      猜你喜欢
      • 2012-04-08
      • 1970-01-01
      • 2017-10-03
      • 2011-01-01
      • 1970-01-01
      • 2016-03-14
      • 2012-09-06
      • 2015-10-28
      相关资源
      最近更新 更多