gpt4 book ai didi

java - Spring Security LDAP 身份验证用户必须是 AD 组的成员

转载 作者:搜寻专家 更新时间:2023-10-31 08:22:09 25 4
gpt4 key购买 nike

我已经按照以下方式配置了 Spring Boot Security: https://spring.io/guides/gs/securing-web/

我可以使用我的凭据完美登录。但是,我需要添加检查以确保 AD 用户也必须属于特定的 AD 组(即 AD-this-is-a-specific-group)。 登录时,如果用户不属于特定的AD组,则返回登录错误。

我已经搜索了几个小时,但似乎无法在 WebSecurityConfigurerAdapter 中找到明确的方法,我是否正确使用了 auth.groupSearchFilter

这是我的代码:

@Configuration 
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
Environment env;

public LdapContextSource contextSource () {
LdapContextSource contextSource= new LdapContextSource();

contextSource.setUrl(env.getRequiredProperty("ldap.url"));
contextSource.setBase(env.getRequiredProperty("ldap.baseDn"));
contextSource.setUserDn(env.getRequiredProperty("ldap.bindDn"));
contextSource.setPassword(env.getRequiredProperty("ldap.batchPassword"));
contextSource.afterPropertiesSet();
return contextSource;
}

@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.ldapAuthentication()
.userSearchFilter("(cn={0})")
.groupSearchBase("OU=Account Groups,OU=ITS Security")
.groupSearchFilter("(cn=AD-this-is-a-specific-group)")
.contextSource(contextSource());
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().fullyAuthenticated()
.and()
.formLogin();
}

最佳答案

我很抱歉在聚会上迟到了 5 年,但我在 Spring Boot 中实现的非常简单的 LDAP 身份验证遇到了完全相同的问题。

我只想要这个:- 用户名正确吗?- 密码正确吗?- 如果是,usr 是否在组 MYGROUP 中?

所以我的配置方法现在看起来非常小。我在一个单独的 bean 中添加了填充器,只是意识到我需要将它添加到“auth.ldapAuthentication”中以便调用它。

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {

auth.ldapAuthentication()
.userSearchFilter("uid={0}")
.ldapAuthoritiesPopulator(ldapAuthoritiesPopulator())
.groupSearchFilter("(member={0})")
.contextSource(contextSource());
}

@Bean
public LdapAuthoritiesPopulator ldapAuthoritiesPopulator() {

DefaultLdapAuthoritiesPopulator populi = new DefaultLdapAuthoritiesPopulator(contextSource(), "") {

@Override
public Set<GrantedAuthority> getGroupMembershipRoles(String userDn, String username) {
Set<GrantedAuthority> groupMembershipRoles = super.getGroupMembershipRoles(userDn, username);

boolean isMemberOfSpecificAdGroup = false;
for (GrantedAuthority grantedAuthority : groupMembershipRoles) {

if ("ROLE_MYGROUP".equals(grantedAuthority.toString())) {
isMemberOfSpecificAdGroup = true;
break;
}
}

if (!isMemberOfSpecificAdGroup) {

throw new BadCredentialsException("User must be a member of " + "ROLE_MYGROUP");
}
return groupMembershipRoles;
}
};

return populi;
}

@Bean
public DefaultSpringSecurityContextSource contextSource() {
return new DefaultSpringSecurityContextSource("ldap://blabla-some-url:389/dc=something,dc=something,dc=ch");
}

顺便说一句:该 url 没有像 Spring Boot guide 中提到的那样工作它只是这样工作的,就像一行中的所有内容:

return new DefaultSpringSecurityContextSource("ldap://blabla-some-url:389/dc=something,dc=something,dc=ch");

顺便说一句,对于遵循该指南的每个人:如果您连接到一个已经存在的 LDAP 服务器,您不需要所有那些“spring.ldap.embedded”应用程序属性。

非常感谢您的帮助!

关于java - Spring Security LDAP 身份验证用户必须是 AD 组的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38319309/

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