【发布时间】:2011-08-02 22:49:53
【问题描述】:
我的应用程序需要我为单个用户定义多个角色。
我已阅读Spring security with database and multiple roles?。
我们为什么要实现我们自己的 UserDetails ?现有一个包含
Collection getAuthorities();
还有我可以遵循的任何参考或教程来为单个用户实现多个角色吗?
【问题讨论】:
标签: spring-security
我的应用程序需要我为单个用户定义多个角色。
我已阅读Spring security with database and multiple roles?。
我们为什么要实现我们自己的 UserDetails ?现有一个包含
Collection getAuthorities();
还有我可以遵循的任何参考或教程来为单个用户实现多个角色吗?
【问题讨论】:
标签: spring-security
您引用的帖子的已接受答案对我来说似乎不正确。您不必为此创建自己的 UserDetailsService 实现。已经支持多个角色。见JdbcDaoImpl。您只需确保authoritiesByUsernameQuery 与您的数据库设置相匹配。默认情况下,它的值为select username,authority from authorities where username = ?。此查询由加载所有权限的loadUserAuthorities 方法执行。
【讨论】:
如果有人对逗号分隔权限的自定义 UserDetailsService 感兴趣:
@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 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"
【讨论】: