Update LDAP samples to use LdapBindAuthenticationManagerFactory

Closes gh-61
This commit is contained in:
Eleftheria Stein 2022-01-31 12:37:36 +01:00
parent a19471b510
commit 2ddf0a2fa9
2 changed files with 29 additions and 53 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,39 +17,29 @@ package example;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.ldap.core.support.BaseLdapPathContextSource; import org.springframework.ldap.core.support.BaseLdapPathContextSource;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.ldap.DefaultSpringSecurityContextSource; import org.springframework.security.config.ldap.EmbeddedLdapServerContextSourceFactoryBean;
import org.springframework.security.ldap.authentication.BindAuthenticator; import org.springframework.security.config.ldap.LdapBindAuthenticationManagerFactory;
import org.springframework.security.ldap.authentication.LdapAuthenticationProvider; import org.springframework.security.ldap.userdetails.PersonContextMapper;
import org.springframework.security.ldap.authentication.LdapAuthenticator;
import org.springframework.security.ldap.server.UnboundIdContainer;
@EnableWebSecurity @EnableWebSecurity
public class SecurityConfiguration { public class SecurityConfiguration {
@Bean @Bean
UnboundIdContainer ldapContainer() { public EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean() {
UnboundIdContainer result = new UnboundIdContainer("dc=springframework,dc=org", "classpath:users.ldif"); EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean = EmbeddedLdapServerContextSourceFactoryBean
result.setPort(0); .fromEmbeddedLdapServer();
return result; contextSourceFactoryBean.setPort(0);
return contextSourceFactoryBean;
} }
@Bean @Bean
DefaultSpringSecurityContextSource contextSource(UnboundIdContainer container) { AuthenticationManager authenticationManager(BaseLdapPathContextSource contextSource) {
return new DefaultSpringSecurityContextSource( LdapBindAuthenticationManagerFactory factory = new LdapBindAuthenticationManagerFactory(contextSource);
"ldap://localhost:" + container.getPort() + "/dc=springframework,dc=org"); factory.setUserDnPatterns("uid={0},ou=people");
} factory.setUserDetailsContextMapper(new PersonContextMapper());
return factory.createAuthenticationManager();
@Bean
BindAuthenticator authenticator(BaseLdapPathContextSource contextSource) {
BindAuthenticator authenticator = new BindAuthenticator(contextSource);
authenticator.setUserDnPatterns(new String[] { "uid={0},ou=people" });
return authenticator;
}
@Bean
LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticator) {
return new LdapAuthenticationProvider(authenticator);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,13 +18,10 @@ package example;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.ldap.core.ContextSource;
import org.springframework.ldap.core.support.BaseLdapPathContextSource; import org.springframework.ldap.core.support.BaseLdapPathContextSource;
import org.springframework.security.ldap.DefaultSpringSecurityContextSource; import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.ldap.authentication.BindAuthenticator; import org.springframework.security.config.ldap.EmbeddedLdapServerContextSourceFactoryBean;
import org.springframework.security.ldap.authentication.LdapAuthenticationProvider; import org.springframework.security.config.ldap.LdapBindAuthenticationManagerFactory;
import org.springframework.security.ldap.authentication.LdapAuthenticator;
import org.springframework.security.ldap.server.UnboundIdContainer;
import org.springframework.security.ldap.userdetails.PersonContextMapper; import org.springframework.security.ldap.userdetails.PersonContextMapper;
/** /**
@ -36,30 +33,19 @@ import org.springframework.security.ldap.userdetails.PersonContextMapper;
public class SecurityConfig { public class SecurityConfig {
@Bean @Bean
UnboundIdContainer ldapContainer() { public EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean() {
UnboundIdContainer container = new UnboundIdContainer("dc=springframework,dc=org", "classpath:users.ldif"); EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean = EmbeddedLdapServerContextSourceFactoryBean
container.setPort(0); .fromEmbeddedLdapServer();
return container; contextSourceFactoryBean.setPort(0);
return contextSourceFactoryBean;
} }
@Bean @Bean
ContextSource contextSource(UnboundIdContainer container) { AuthenticationManager authenticationManager(BaseLdapPathContextSource contextSource) {
int port = container.getPort(); LdapBindAuthenticationManagerFactory factory = new LdapBindAuthenticationManagerFactory(contextSource);
return new DefaultSpringSecurityContextSource("ldap://localhost:" + port + "/dc=springframework,dc=org"); factory.setUserDnPatterns("uid={0},ou=people");
} factory.setUserDetailsContextMapper(new PersonContextMapper());
return factory.createAuthenticationManager();
@Bean
BindAuthenticator authenticator(BaseLdapPathContextSource contextSource) {
BindAuthenticator authenticator = new BindAuthenticator(contextSource);
authenticator.setUserDnPatterns(new String[] { "uid={0},ou=people" });
return authenticator;
}
@Bean
LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticator) {
LdapAuthenticationProvider provider = new LdapAuthenticationProvider(authenticator);
provider.setUserDetailsContextMapper(new PersonContextMapper());
return provider;
} }
} }