【问题标题】:Spring security : Multiple roles for a single userSpring security:单个用户的多个角色
【发布时间】:2011-08-02 22:49:53
【问题描述】:

我的应用程序需要我为单个用户定义多个角色。

我已阅读Spring security with database and multiple roles?。

我们为什么要实现我们自己的 UserDetails ?现有一个包含

Collection getAuthorities();

还有我可以遵循的任何参考或教程来为单个用户实现多个角色吗?

【问题讨论】:

    标签: spring-security


    【解决方案1】:

    您引用的帖子的已接受答案对我来说似乎不正确。您不必为此创建自己的 UserDetailsService 实现。已经支持多个角色。见JdbcDaoImpl。您只需确保authoritiesByUsernameQuery 与您的数据库设置相匹配。默认情况下,它的值为select username,authority from authorities where username = ?。此查询由加载所有权限的loadUserAuthorities 方法执行。

    【讨论】:

    • 我试过了,我没有将 ROLE_ADMIN,ROLE_USER 分成 2 个角色,而是将整个字符串作为一个角色“ROLE_ADMIN,ROLE_USER”,不用说它不起作用
    • @markbaldy 我不确定我是否遇到了问题。您可以添加代码来显示您尝试过的内容吗?
    • 使用您的解决方案,整个字符串作为单个权限返回:“ROLE_ADMIN,ROLE_USER”,而不是 ["ROLE_ADMIN","ROLE_USER"]。我确实需要实现自己的 UserDetailsS​​ervice。
    • 我想我想知道你从哪里得到那个字符串。 JdbcDaoImpl 的 loadUserAuthorities 方法返回一个 GrantedAuthority 列表。
    【解决方案2】:

    如果有人对逗号分隔权限的自定义 UserDetailsS​​ervice 感兴趣:

    @Component
    public class MyUserDetailsService implements UserDetailsService {
    
        @Resource
        private AccountService accounts;
    
        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    
            Account account = accounts.findByUsername(username);
            if(null == account) {
                throw new UsernameNotFoundException("User " + username + " not found.");
            }
    
            List<SimpleGrantedAuthority> authorities = new ArrayList<SimpleGrantedAuthority>();
            String[] authStrings = account.getAuthorities().split(", ");
            for(String authString : authStrings) {
                authorities.add(new SimpleGrantedAuthority(authString));
            }
    
            UserDetails ud = new User(account.getUsername(), account.getPassword(), authorities);
            return ud;
        }
    
    }
    

    现在你可以在 db 中有这样的东西:

    +----+-----------------------+----------+----------+
    | id | authorities           | password | username |
    +----+-----------------------+----------+----------+
    |  1 | ROLE_ADMIN            | 123qwe   | markm    |
    |  2 | ROLE_ADMIN, ROLE_USER | 123qwe   | kemika   |
    +----+-----------------------+----------+----------+
    

    【讨论】:

    • 如何配置spring config文件以支持为单个用户分配多个角色?
    【解决方案3】:

    Spring security 支持多个角色开箱即用!

    所以,为了节省你们所有人的大量时间:

    必须为同一用户插入多个条目: 那是在 MySQL Workbench 中,使用 MySQL 5.7.24 还有其他环境 - 如果您想知道要重现该结果的版本:

        <!-- Inherit defaults from Spring Boot -->
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.1.RELEASE</version>
        </parent>
    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-security</artifactId>
            </dependency>
            <!-- optional, it brings useful tags to display spring security stuff -->
            <dependency>
                <groupId>org.thymeleaf.extras</groupId>
                <artifactId>thymeleaf-extras-springsecurity5</artifactId>
            </dependency>
    

    然后验证我制作了这个页面并且:

    这是显示和验证登录帐户权限的示例代码:

    <div data-layout-fragment="content" class="content">
        <div class="row mt-4">
        <div class="col-md-12">
            <h2>Show Authorities Glance</h2>
            <div class="card">
            <div class="card-body">
                Logged user: <span data-sec-authentication="name">Bob</span>
                Roles: <span data-sec-authentication="principal.authorities">[ROLE_USER, ROLE_ADMIN]</span>
                <div data-sec-authorize="isAuthenticated()">
                This content is only shown to authenticated users.
                </div>
                <div data-sec-authorize="hasRole('ROLE_USER')">
                This content is only shown to ROLE_USER.
                </div>
                <div data-sec-authorize="hasRole('ROLE_EMPLOYEE')">
                This content is only shown to ROLE_EMPLOYEE.
                </div>
                <div data-sec-authorize="hasRole('ROLE_FOUNDER')">
                This content is only shown to ROLE_FOUNDER.
                </div>
                <div data-sec-authorize="hasRole('ROLE_ADMIN')">
                This content is only shown to ROLE_ADMIN.
                </div>
            </div>
            </div>
        </div>
        </div>
    </div>
    <!--<p>-->
        <!--<a data-th-href="@{/add-authority}">Add a new authority</a>-->
    <!--</p>-->
    </div>
    

    哦,最后一个视图包含百里香叶,不仅是标准视图,还有布局方言。以防万一你想尝试一下也需要这个依赖:

    <dependency>
        <groupId>nz.net.ultraq.thymeleaf</groupId>
        <artifactId>thymeleaf-layout-dialect</artifactId>
    </dependency>
    

    或者获取布局片段标签:

    data-layout-fragment="content"
    

    【讨论】:

      猜你喜欢
      • 2014-01-31
      • 2012-06-08
      • 2016-10-14
      • 2019-12-19
      • 2019-12-29
      • 2015-08-15
      • 2012-09-15
      • 1970-01-01
      • 2011-07-21
      相关资源
      最近更新 更多