gpt4 book ai didi

java - Spring Security - GrantedAuthoritiesMapper 更改未持久

转载 作者:太空宇宙 更新时间:2023-11-04 06:36:26 27 4
gpt4 key购买 nike

我正在使用 Spring Security 的 Active Directory 实现。我创建了一个自定义的 GrantedAuthoritiesMapper 将 AD 角色映射到我的应用程序的角色。不过,当局的改变似乎并没有发生。

我的配置:

<b:bean id="grantedAuthoritiesMappers"
class="com.xxx.TestAuthoritiesMapper"/>

<b:bean id="adAuthenticationProvider"
class="org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider">
<b:constructor-arg value="MYDOMAIN" />
<b:constructor-arg value="ldap://192.168.2.4/" />
<b:property name="authoritiesMapper" ref="grantedAuthoritiesMappers"></b:property>
</b:bean>

<authentication-manager>
<authentication-provider ref="adAuthenticationProvider" />
</authentication-manager>

映射器代码:

public class TestAuthoritiesMapper implements GrantedAuthoritiesMapper {

private static Logger logger = Logger
.getLogger(GrantedAuthoritiesMapper.class.toString());

@Override
public Collection<? extends GrantedAuthority> mapAuthorities(
Collection<? extends GrantedAuthority> authorities) {

logger.info("Coming in :" + authorities);

Set newAuths = new HashSet();
newAuths.addAll(authorities);
newAuths.add(new SimpleGrantedAuthority("Custom Role"));

logger.info("Coming out :" + newAuths);

return newAuths;

}
}

在这里我可以看到我的权威增强列表。后来,当我运行时:

    Object principal = this.getSecurityContext().getAuthentication().getPrincipal();
UserDetails ud = (UserDetails) principal;
log.info("Final auths:" + ud.getAuthorities());

我只看到原始授权,没有我添加的额外内容。

当我运行调试器时,修改后的身份验证一直存在,直到 Spring 发布成功的身份验证对象。在被触发的事件和我的安全上下文之间的某个位置,更改丢失了。

最佳答案

我通过创建自定义类解决了这个问题

我能够通过重写方法mapUserFromContext来获取用户并更新其权限。希望这会有所帮助。

public class CustomUserDetailsMapper extends LdapUserDetailsMapper 
implements UserDetailsContextMapper {
...
@Override
public UserDetails mapUserFromContext(DirContextOperations ctx,
String username, Collection<? extends GrantedAuthority> authorities) {

UserDetails user = super.mapUserFromContext(ctx, username,
updateAuthorities(username));
...
}

关于java - Spring Security - GrantedAuthoritiesMapper 更改未持久,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25320011/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com