gpt4 book ai didi

java - 如何配置 spring 使用外部 LDAP 服务器

转载 作者:行者123 更新时间:2023-11-30 06:01:41 25 4
gpt4 key购买 nike

我正在学习 Spring Security 到 LDAP 服务器,现在我正在尝试使 Spring 对 LDAP 服务器进行身份验证。但是,spring始终使用嵌入式服务器ldap://127.0.0.1:33389/dc=springframework,dc=org而不是我的ldap://localhost:389/dc=localdomain, dc=本地。我正在尝试使用 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"
);
}
}

应用程序属性

#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,仍然不起作用。

应用程序属性

#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 但它总是使用 spring 中的嵌入式 ldap

最佳答案

您可以采用与 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

关于java - 如何配置 spring 使用外部 LDAP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52153346/

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