我正在了解Spring Security到LDAP服务器的信息,现在我正在尝试使Spring对ldap服务器进行身份验证。但是,spring始终使用嵌入式服务器ldap://127.0.0.1:33389/dc=springframework,dc=org
而不是我的ldap://localhost:389/dc=localdomain,dc=local
。我正在尝试使用application.properties
对其进行配置,请参见下面的spring配置。
WebSecurityConfig.java
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private static final Logger logger = LoggerFactory.getLogger(WebSecurityConfig.class);
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
logger.info("Loading Global Auth Configuration");
auth
.ldapAuthentication();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
logger.info("Configuring HTTP Security.");
// Configure Web Security
http
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/auth/**").permitAll()
.anyRequest().authenticated();
// disable page caching
http.headers().cacheControl();
}
@Override
public void configure(WebSecurity web) throws Exception {
logger.info("Configuring Web Security HTTP Security.");
// AuthenticationTokenFilter will ignore the below paths
web
.ignoring()
.antMatchers(
HttpMethod.POST,
"/auth"
);
}
}
application.properties
#Ldap Info
spring.ldap.urls=ldap://localhost:389
spring.ldap.anonymous-read-only=true
spring.ldap.username=ldapadm
spring.ldap.password=root123
spring.ldap.base=ou=People,dc=localdomain,dc=local
尝试使用以上application.properties
,仍然无法正常工作。
application.properties
#Ldap Info
ldap.urls=ldap://localhost:389
ldap.base.dn=dc=localdomain,dc=local
ldap.username=cn=ldapadm,dc=localdomain,dc=local
ldap.password=root123
ldap.user.dn.pattern =uid={0}
我也尝试了上述属性,但仍然无法正常工作。
2018-09-04 00:05:31.515 INFO 9948 --- [ main] s.s.l.DefaultSpringSecurityContextSource : URL 'ldap://127.0.0.1:33389/dc=springframework,dc=org', root DN is 'dc=springframework,dc=org'
2018-09-04 00:05:31.516 INFO 9948 --- [ main] o.s.l.c.support.AbstractContextSource : Property 'userDn' not set - anonymous context will be used for read-write operations
2018-09-04 00:05:31.523 WARN 9948 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.RuntimeException: Could not postProcess org.springframework.security.ldap.authentication.BindAuthenticator@3bc735b3 of type class org.springframework.security.ldap.authentication.BindAuthenticator
2018-09-04 00:05:31.526 INFO 9948 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
对于application.properties
中的两个设置,我总是在服务器日志中得到它
有人能理解这些吗?我试图使其读取application.properties
,但它在春季始终使用嵌入式ldap
答案 0 :(得分:1)
您可以采用与LDAP Authentication with Spring Boot
类似的方法在application.properties中。
ldap.urls=ldap://localhost:389/dc=localdomain,dc=local
在您的WebSecurityConfig中
@Value("${ldap.urls:ldap://127.0.0.1:33389/dc=springframework,dc=org}")
private String ldapUrls;
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.ldapAuthentication()
.userDnPatterns("uid={0},ou=people")
.groupSearchBase("ou=groups")
.contextSource()
.url(ldapUrls)
.and()
.passwordCompare()
.passwordEncoder(new LdapShaPasswordEncoder())
.passwordAttribute("adminpassword");
}
请注意,实际参数(userDnPatterns等...)可能会根据您的LDAP配置进行更改,我只是指出了如何配置LDAP配置以连接到外部LDAP