fla*_*ash 76 java spring-security
我正在尝试使用权限设置基于角色的安全性.我正在尝试与Spring-Security一起完成这项工作.
我不想设置ACL,因为它似乎对我的要求有点过分.
我只想拥有本文所述的简单权限和角色.遗憾的是,该文章没有描述如何实现给定的解决方案.
有人已经尝试过这个并且可以指出我正确的方向吗?也许还有另一篇描述实施的博客文章?
非常感谢你.
Wil*_*ler 76
我是这篇文章的作者.
毫无疑问,有多种方法可以做到这一点,但我通常采用的方式是实现UserDetails
了解角色和权限的自定义.Role
并且Permission
只是您编写的自定义类.(没什么特别的 - Role
有一个名称和一组Permission
实例,并且Permission
有一个名字.)然后getAuthorities()
返回如下所示的GrantedAuthority
对象:
PERM_CREATE_POST
,PERM_UPDATE_POST
,PERM_READ_POST
而不是返回像
ROLE_USER
, ROLE_MODERATOR
如果您的UserDetails
实现具有getRoles()
方法,则角色仍然可用.(我建议有一个.)
理想情况下,您可以为用户分配角色,并自动填充关联的权限.这将涉及拥有一个UserDetailsService
知道如何执行该映射的自定义,并且它所要做的就是从数据库中获取映射.(请参阅有关架构的文章.)
然后,您可以根据权限而不是角色来定义授权规则.
希望有所帮助.
小智 30
要实现这一点,您似乎必须:
org.springframework.security.authentication.ProviderManager
并将其配置(将其提供者设置)为自定义org.springframework.security.authentication.AuthenticationProvider
.最后一个应该在其身份验证方法上返回一个身份验证org.springframework.security.core.GrantedAuthority
,在您的情况下,应该使用给定用户的所有权限进行设置.该文章的技巧是将角色分配给用户,但是,为Authentication.authorities
对象中的那些角色设置权限.
为此,我建议您阅读API,看看是否可以扩展一些基本的ProviderManager和AuthenticationProvider,而不是实现所有内容.我已经完成了org.springframework.security.ldap.authentication.LdapAuthenticationProvider
设置自定义LdapAuthoritiesPopulator,它将为用户检索正确的角色.
希望这次我得到了你想要的东西.祝好运.
基本步骤是:
使用自定义身份验证提供程
<bean id="myAuthenticationProvider" class="myProviderImplementation" scope="singleton">
...
</bean>
Run Code Online (Sandbox Code Playgroud)
使您的自定义提供程序返回自定义UserDetails
实现.这UserDetailsImpl
将是getAuthorities()
这样的:
public Collection<GrantedAuthority> getAuthorities() {
List<GrantedAuthority> permissions = new ArrayList<GrantedAuthority>();
for (GrantedAuthority role: roles) {
permissions.addAll(getPermissionsIncludedInRole(role));
}
return permissions;
}
Run Code Online (Sandbox Code Playgroud)当然,从这里开始,您可以根据具体要求应用大量优化/自定义.
这是最简单的方法.允许组权限以及用户权限.
-- Postgres syntax
create table users (
user_id serial primary key,
enabled boolean not null default true,
password text not null,
username citext not null unique
);
create index on users (username);
create table groups (
group_id serial primary key,
name citext not null unique
);
create table authorities (
authority_id serial primary key,
authority citext not null unique
);
create table user_authorities (
user_id int references users,
authority_id int references authorities,
primary key (user_id, authority_id)
);
create table group_users (
group_id int references groups,
user_id int referenecs users,
primary key (group_id, user_id)
);
create table group_authorities (
group_id int references groups,
authority_id int references authorities,
primary key (group_id, authority_id)
);
Run Code Online (Sandbox Code Playgroud)
然后在META-INF/applicationContext-security.xml中
<beans:bean class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" id="passwordEncoder" />
<authentication-manager>
<authentication-provider>
<jdbc-user-service
data-source-ref="dataSource"
users-by-username-query="select username, password, enabled from users where username=?"
authorities-by-username-query="select users.username, authorities.authority from users join user_authorities using(user_id) join authorities using(authority_id) where users.username=?"
group-authorities-by-username-query="select groups.id, groups.name, authorities.authority from users join group_users using(user_id) join groups using(group_id) join group_authorities using(group_id) join authorities using(authority_id) where users.username=?"
/>
<password-encoder ref="passwordEncoder" />
</authentication-provider>
</authentication-manager>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
101166 次 |
最近记录: |