gpt4 book ai didi

java - Spring LDAP 角色映射

转载 作者:太空宇宙 更新时间:2023-11-04 13:14:55 33 4
gpt4 key购买 nike

我关注了this article并将我的应用程序配置为通过 LDAP 进行身份验证(工作正常)。现在我在应用程序中仅使用 3 个角色,我想为它们创建映射。

所以我实现了接口(interface)GrantedAuthoritiesMapper

@Component
public class MyAuthorityMapper implements GrantedAuthoritiesMapper {

@Autowired
private MyAuthorityConfig authoritiesConfig;

@Override
public Collection<? extends GrantedAuthority> mapAuthorities(Collection<? extends GrantedAuthority> collection) {
Set<MyAuthority> roles = EnumSet.noneOf(MyAuthority.class);

for (GrantedAuthority g : collection) {
for (String role : authoritiesConfig.getAuthoritiesMap().keySet()) {
if (Arrays.asList(authoritiesConfig.getAuthoritiesMap().get(role).split(",")).contains(g.getAuthority())) {
roles.add(MyAuthority.valueOf(role));
}
}
}
return roles;
}
}

这是角色填充器

@Component
@ConfigurationProperties(prefix = "auth.role.mapping")
public class MyAuthorityConfig {

private Map<String, String> authroritiesMap = new HashMap<String, String>();

public Map<String, String> getAuthoritiesMap() {
return this.authroritiesMap;
}
}

和application-dev.properties

auth.role.mapping.ROLE_COMPETENCE_CENTER=ROLECC
auth.role.mapping.ROLE_OPERATIONS=ROLEOPS,ROLEPAR
auth.role.mapping.ROLE_ADMINISTRATOR=ROLEADM,ROLESUPUSR

现在 MyAuthorityConfig 仅包含空 map 。是否可以像我在这里使用的那样使用 @ConfigurationProperties ?我找不到如何用它填充 map 。或者配置文件特定的属性文件是否存在问题?

在 WebSecurityConfig 中,我有 LDAP 配置的方法,但我不知道如何/在哪里注入(inject) MyAuthorityMapper 或者是否可以不使用 ActiveDirectoryLdapAuthenticationProvider

private void configureLdap(AuthenticationManagerBuilder auth) throws Exception {
DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource(env.getProperty("auth.ldap.urls"));
contextSource.setUserDn(env.getProperty("auth.ldap.user"));
contextSource.setPassword(env.getProperty("auth.ldap.password"));
contextSource.setReferral("follow");
contextSource.afterPropertiesSet();

auth.ldapAuthentication()
.userSearchBase(env.getProperty("auth.ldap.user.search.base"))
.userSearchFilter(env.getProperty("auth.ldap.user.search.filter"))
.groupSearchBase(env.getProperty("auth.ldap.group.search.base"))
.groupSearchFilter(env.getProperty("auth.ldap.group.search.filter"))
.groupRoleAttribute(env.getProperty("auth.ldap.group.search.attribute"))
.contextSource(contextSource)
;
}

最佳答案

好的,对于 @ConfigurationProperties 的第一个问题,有这样的修复:

@Component
@ConfigurationProperties(prefix = "auth.role")
public class MyAuthorityConfig {

private Map<String, String> mapping = new HashMap<String, String>();

public Map<String, String> getMapping() {
return this.mapping;
}
}

@ConfigurationProperties 在属性中查找前缀 auth.role,然后获取映射部分,该部分应该是我的类中的属性名称。

对于第二个问题,我找到了 UserDetailsContextMapper

的解决方案
@Component(value = "myUserDetailsContextMapper")
public class MyUserDetailsContextMapper implements UserDetailsContextMapper {

private static final Logger log = LoggerFactory.getLogger(MyUserDetailsContextMapper.class);

@Autowired
private MyAuthorityConfig authoritiesConfig;

@Override
public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<? extends GrantedAuthority> authorities) {
log.debug("mapUserFromContext start");
List<GrantedAuthority> mappedAuthorities = new ArrayList<>();

for (GrantedAuthority g : authorities) {
for (String role : authoritiesConfig.getMapping().keySet()) {
if (Arrays.asList(authoritiesConfig.getMapping().get(role).split(","))
.contains(g.getAuthority().startsWith("ROLE_") ? g.getAuthority().substring("ROLE_".length()) : g.getAuthority())) {
log.debug("Mapping from LDAP role {} to application role {} for user {}", g.getAuthority(), role, username);
mappedAuthorities.add(MyAuthority.valueOf(role));
}
}
}

return new User(username, "", mappedAuthorities);
}

@Override
public void mapUserToContext(UserDetails user, DirContextAdapter ctx) {

}
}

我不确定仅返回 new User(username, "",mappedAuthorities); 是否可以(我必须使用锁定/禁用的用户正确测试它),但现在它可以工作。

关于java - Spring LDAP 角色映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33635415/

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