【发布时间】:2016-11-14 03:08:04
【问题描述】:
我已经按照以下方式配置了 Spring Boot 安全性: 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();
}
【问题讨论】:
标签: java spring-security spring-boot ldap spring-ldap