Car*_*eon 5 java ldap spring-security spring-ldap spring-boot
我按照以下方式配置了Spring Boot Security:https: //spring.io/guides/gs/securing-web/
我能够完美地使用我的凭据登录.但是,我需要添加一个检查,即AD用户也必须属于特定的AD组(即AD-this-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();
}
Run Code Online (Sandbox Code Playgroud)
不确定这是否是最好的方法(就Spring Security的生命周期而言),但基本上我提供了自己的DefaultLdapAuthoritiesPopulator,我只覆盖了getGroupMembershipRoles.
首先,我auth.groupSearchFilter上面有错,它应该是:
.groupSearchFilter("(member={0})")
Run Code Online (Sandbox Code Playgroud)
其次,我创建了一个带有重写方法的匿名类(调用super并检查角色列表中的成员资格):
auth
.ldapAuthentication()
.ldapAuthoritiesPopulator(new DefaultLdapAuthoritiesPopulator(contextSource, "OU=Account Groups,OU=ITS Security") {
@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_AD-this-is-a-specific-group".equals(grantedAuthority.toString())) {
isMemberOfSpecificAdGroup = true;
break;
}
}
if (!isMemberOfSpecificAdGroup ) {
throw new BadCredentialsException("User must be a member of " + "AD-this-is-a-specific-group");
}
return groupMembershipRoles;
}
})
.userSearchFilter("(cn={0})")
.groupSearchBase("OU=Account Groups,OU=ITS Security")
.groupSearchFilter("(member={0})")
.contextSource(contextSource);
Run Code Online (Sandbox Code Playgroud)
对于参加晚会晚了5年,我感到抱歉,但是我在Spring Boot中实现的非常简单的LDAP身份验证存在完全相同的问题。
我只想要这样:-它是正确的用户名吗?-密码正确吗?-如果是,则MYGROUP组中的usr是吗?
因此,我的configure方法现在看起来很小。我在一个单独的bean中添加了populator,只是意识到我需要在“ 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");
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句:url不能像Spring Boot指南中提到的那样工作,只能像这样,就像一行中的所有内容一样工作:
return new DefaultSpringSecurityContextSource("ldap://blabla-some-url:389/dc=something,dc=something,dc=ch");
Run Code Online (Sandbox Code Playgroud)
顺便说一下,对于每个遵循该指南的人:如果连接到现有的LDAP服务器,则不需要所有那些“ spring.ldap.embedded”应用程序属性。
因此,非常感谢您的帮助!
| 归档时间: |
|
| 查看次数: |
5990 次 |
| 最近记录: |