Spring security Spring安全性:单个用户的多个角色

Spring security Spring安全性:单个用户的多个角色,spring-security,Spring Security,我的应用程序需要我为一个用户定义多个角色 我读过 我们为什么要实现自己的用户详细信息?现有的一个包含 Collection getAuthorities(); 另外,对于为一个用户实现多个角色,是否有任何参考或教程可供我遵循?对于您引用的帖子,我认为公认的答案不正确。您不必为此创建自己的UserDetailsService实现。已支持多个角色。看见您只需确保authoritiesByUsernameQuery与您的数据库设置匹配即可。默认情况下,其值为选择用户名、来自授权机构的授权,其中用户名

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

我读过

我们为什么要实现自己的用户详细信息?现有的一个包含

Collection getAuthorities();

另外,对于为一个用户实现多个角色,是否有任何参考或教程可供我遵循?

对于您引用的帖子,我认为公认的答案不正确。您不必为此创建自己的
UserDetailsService
实现。已支持多个角色。看见您只需确保
authoritiesByUsernameQuery
与您的数据库设置匹配即可。默认情况下,其值为
选择用户名、来自授权机构的授权,其中用户名=?
。此查询由加载所有权限的
LoadUserAuthories
方法执行。

如果任何人对逗号分隔权限的自定义UserDetails服务感兴趣:

@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;
    }

}

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>

我尝试了这个方法,并没有将ROLE\u ADMIN,ROLE\u USER拆分为两个角色,而是将整个字符串作为单个角色“ROLE\u ADMIN,ROLE\u USER”,不用说,它没有work@markbaldy我不确定我是否得到了这个问题。您可以添加代码来显示您尝试过的内容吗?使用您的解决方案,整个字符串作为单个权限返回:“ROLE_ADMIN,ROLE_USER”,而不是[“ROLE_ADMIN”,“ROLE_USER”]。我确实需要实现我自己的UserDetailsService。我想我正在试图理解从哪里获得该字符串。JdbcDaoImpl的LoadUserAuthories方法返回一个GrantedAuthority的列表。您如何准确地配置spring配置文件以支持向单个用户分配多个角色?
    <!-- 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"