java-具有角色和权限的Spring Security
我正在尝试设置具有权限的基于角色的安全性。 我正在尝试与Spring-Security一起执行此操作。
我不想设置ACL,因为这似乎对我的要求来说有些过头了。
我只想拥有本文所述的简单权限和角色。不幸的是,本文没有描述如何实现给定的解决方案。
有人已经尝试过了,可以指出正确的方向吗? 也许还有另一个博客条目描述了实现方式?
非常感谢你。
flash asked 2019-09-22T15:28:26Z
5个解决方案
73 votes
我是有关文章的作者。
毫无疑问,有多种方法可以执行此操作,但是我通常采用的方法是实现自定义UserDetailsService,该功能知道角色和权限。 getRoles()和PERM_READ_POST只是您编写的自定义类。 (fancy--Role没有名称和一组Permission实例,Permission没有名称。)然后getAuthorities()返回GrantedAuthority对象,如下所示:
UserDetailsService、getRoles()、PERM_READ_POST
而不是返回诸如
UserDetailsService、getRoles()
如果您的UserDetailsService实现具有getRoles()方法,则这些角色仍然可用。 (我建议有一个。)
理想情况下,您将角色分配给用户,并且关联的权限会自动填写。 这将涉及拥有一个知道如何执行该映射的自定义UserDetailsService,而它所要做的就是从数据库中获取映射。 (有关架构,请参见文章。)
然后,您可以根据权限而不是角色来定义授权规则。
希望有所帮助。
Willie Wheeler answered 2019-09-22T15:30:44Z
30 votes
要实现这一点,您似乎必须:
创建模型(用户,角色,权限)以及检索给定用户权限的方法;
定义自己的org.springframework.security.ldap.authentication.LdapAuthenticationProvider,并将其配置(设置其提供程序)为自定义org.springframework.security.authentication.AuthenticationProvider。最后一个应该在其authenticate方法上返回Authentication,该身份应该使用2537191713681179179650进行设置(在您的情况下,应设置为给定用户的所有权限)。
该文章中的技巧是为用户分配角色,但是要在org.springframework.security.ldap.authentication.LdapAuthenticationProvider对象中为这些角色设置权限。
为此,我建议您阅读API,并查看是否可以扩展一些基本的ProviderManager和AuthenticationProvider而不是全部实现。 我已经完成了org.springframework.security.ldap.authentication.LdapAuthenticationProvider设置自定义LdapAuthoritiesPopulator的操作,该操作将为用户检索正确的角色。
希望这次我能得到您想要的。祝好运。
ezequielb answered 2019-09-22T15:29:20Z
6 votes
基本步骤是:
使用自定义身份验证提供程序
...
使您的自定义提供程序返回自定义public Collection getAuthorities() {
List permissions = new ArrayList();
for (GrantedAuthority role: roles) {
permissions.addAll(getPermissionsIncludedInRole(role));
}
return permissions;
}实现。 该UserDetailsImpl将具有getAuthorities(),如下所示:
public Collection getAuthorities() {
List permissions = new ArrayList();
for (GrantedAuthority role: roles) {
permissions.addAll(getPermissionsIncludedInRole(role));
}
return permissions;
}
当然,您可以从此处针对您的特定要求进行大量优化/自定义。
gpeche answered 2019-09-22T15:31:57Z
5 votes
这是最简单的方法。 允许组权限以及用户权限。
-- 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)
);
然后在META-INF / applicationContext-security.xml中
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=?"
/>
Neil McGuigan answered 2019-09-22T15:32:38Z
0 votes
仅出于完整性考虑(也许其他人不必从头开始实现它):
和其他所有人一样,我们已经实现了自己的小型图书馆。 它应该使事情变得容易,以便我们的开发人员不必每次都重新实现它。 如果Spring Security可以提供现成的rbac支持,那就太好了,因为这种方法比基于默认权限的方法好得多。
查看Github(OSS,MIT许可证),看看它是否适合您的需求。 基本上,它仅解决角色特权映射。 您必须自己提供的缺失部分基本上是用户角色映射,例如 通过将组(种族/广告组)映射到角色(1:1)或实施其他映射。 每个项目都不同,因此提供一些实现没有任何意义。
我们基本上在内部使用了它,因此我们可以从一开始就使用rbac。 如果应用程序正在增长,我们以后仍可以用其他一些实现方式替换它,但是对于我们而言,一开始就正确设置是很重要的。
如果您不使用rbac,则很有可能将权限分散在整个代码库中,并且以后将很难提取(或将它们分组为角色)。生成的图形也确实有助于推理/稍后对其进行重构。
Alexander Pilch answered 2019-09-22T15:33:43Z