我有一个使用 spring-security-core 和 spring-security-ldap 的 grails 应用程序,并通过 Active Directory 进行身份验证。我有一个自定义 UserDetails
和 UserDetailsContextMapper
.
我有一个用例,用户可以临时承担单个 session 的额外责任。我希望能够在用户登录后(即 session 中)为其分配角色,然后在 session 到期时删除该角色。
我有一个 user_role
存储用户和角色之间关系的数据库表。在这种情况下,关系是添加到数据库然后在 session 到期时删除,还是仅存在于内存中都没有关系。无论哪种方式,我都在寻找一种在用户登录后分配角色(并立即应用)的方法。这可能吗?
最佳答案
UserDetails.getAuthorities()
拥有当前登录用户的所有角色。因为您已经有一个自定义 UserDetails
您只需要将附加角色添加到 getAuthorities()
返回的值中。在 session 中期开始时,然后在 session 中期结束时将其删除。 (见 编辑 下面)
IE:
public class MyUserDetails implements UserDetails {
// holds the authorities granted to the user in DB
private List<GrantedAuthority> authorities;
// holds the temporarily added authorities for the mid-session
private List<GrantedAuthority> extraAuthorities;
public GrantedAuthority[] getAuthorities() {
List<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();
auths.addAll(this.authorities);
auths.addAll(this.extraAuthorities);
return auths;
}
public void addExtraAuthorities(GrantedAuthority...auths) {
for (GrantedAuthority a : auths) {
this.extraAuthorities.add(a);
}
}
public void clearExtraAuthorities() {
this.extraAuthorities.clear();
}
}
或者,您可以围绕原始 UserDetails 创建一个包装器,该包装器将保存额外的权限并将其包装起来,并在 session 中期开始时设置为当前的 Authentication 对象,并在 session 中期结束时展开。
编辑:
正如 Ben 在评论中指出的
UserDetails.getAuthorities()
当 Authentication
时,在登录时只会被调用一次对象已创建 - 我忘记了这一点。但这给我们带来了正确的答案,这一次我确定它会起作用,因为我自己就是这样做的。 Authentication.getAuthorities()
是要注意的方法,而不是UserDetails.getAuthorities()
.我真的推荐一个包装器,而不是 Authentication
的自定义实现交互,因为它将更加灵活,并允许透明地支持下面的不同身份验证机制。IE:
public class MidSessionAuthenticationWrapper implements Authentication {
private Authentication wrapped;
private List<GrantedAuthority> authorities;
public MidSessionAuthenticationWrapper(
Authentication wrapped, Collection<GrantedAuthority> extraAuths) {
this.wrapped = wrapped;
this.authorities = new ArrayList<GrantedAuthority>(wrapped.getAuthorities());
this.authorities.addAll(extraAuths);
}
public Authentication getWrapped() {
return this.wrapped;
}
@Override
public Collection<GrantedAuthority> getAuthorities() {
return this.authorities;
}
// delegate all the other methods of Authentication interace to this.wrapped
}
然后你需要做的就是在 session 中期开始:
Authentication authentication =
SecurityContextHolder.getContext().getAuthentication();
Collection<GrantedAuthority> extraAuths =
null; // calculate the extra authorities
MidSessionAuthenticationWrapper midSessionAuthentication =
new MidSessionAuthenticationWrapper(authentication, extraAuths);
SecurityContextHolder.getContext().setAuthentication(midSessionAuthentication);
在 session 中期结束时:
MidSessionAuthenticationWrapper midSessionAuthentication =
(MidSessionAuthenticationWrapper) SecurityContextHolder.getContext().getAuthentication();
Authentication authentication = midSessionAuthentication.getWrapped();
SecurityContextHolder.getContext().setAuthentication(authentication);
关于spring - Grails + Spring 安全核心 : How to assign a role after user has logged in?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7589092/