diff --git a/servlet/java-configuration/authentication/username-password/ldap/src/main/java/example/SecurityConfiguration.java b/servlet/java-configuration/authentication/username-password/ldap/src/main/java/example/SecurityConfiguration.java index 32840f1..8d48f1e 100644 --- a/servlet/java-configuration/authentication/username-password/ldap/src/main/java/example/SecurityConfiguration.java +++ b/servlet/java-configuration/authentication/username-password/ldap/src/main/java/example/SecurityConfiguration.java @@ -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"); * 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.ldap.core.support.BaseLdapPathContextSource; +import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; -import org.springframework.security.ldap.DefaultSpringSecurityContextSource; -import org.springframework.security.ldap.authentication.BindAuthenticator; -import org.springframework.security.ldap.authentication.LdapAuthenticationProvider; -import org.springframework.security.ldap.authentication.LdapAuthenticator; -import org.springframework.security.ldap.server.UnboundIdContainer; +import org.springframework.security.config.ldap.EmbeddedLdapServerContextSourceFactoryBean; +import org.springframework.security.config.ldap.LdapBindAuthenticationManagerFactory; +import org.springframework.security.ldap.userdetails.PersonContextMapper; @EnableWebSecurity public class SecurityConfiguration { @Bean - UnboundIdContainer ldapContainer() { - UnboundIdContainer result = new UnboundIdContainer("dc=springframework,dc=org", "classpath:users.ldif"); - result.setPort(0); - return result; + public EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean() { + EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean = EmbeddedLdapServerContextSourceFactoryBean + .fromEmbeddedLdapServer(); + contextSourceFactoryBean.setPort(0); + return contextSourceFactoryBean; } @Bean - DefaultSpringSecurityContextSource contextSource(UnboundIdContainer container) { - return new DefaultSpringSecurityContextSource( - "ldap://localhost:" + container.getPort() + "/dc=springframework,dc=org"); - } - - @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); + AuthenticationManager authenticationManager(BaseLdapPathContextSource contextSource) { + LdapBindAuthenticationManagerFactory factory = new LdapBindAuthenticationManagerFactory(contextSource); + factory.setUserDnPatterns("uid={0},ou=people"); + factory.setUserDetailsContextMapper(new PersonContextMapper()); + return factory.createAuthenticationManager(); } } diff --git a/servlet/spring-boot/java/ldap/src/main/java/example/SecurityConfig.java b/servlet/spring-boot/java/ldap/src/main/java/example/SecurityConfig.java index d8024ee..82be683 100644 --- a/servlet/spring-boot/java/ldap/src/main/java/example/SecurityConfig.java +++ b/servlet/spring-boot/java/ldap/src/main/java/example/SecurityConfig.java @@ -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"); * 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.Configuration; -import org.springframework.ldap.core.ContextSource; import org.springframework.ldap.core.support.BaseLdapPathContextSource; -import org.springframework.security.ldap.DefaultSpringSecurityContextSource; -import org.springframework.security.ldap.authentication.BindAuthenticator; -import org.springframework.security.ldap.authentication.LdapAuthenticationProvider; -import org.springframework.security.ldap.authentication.LdapAuthenticator; -import org.springframework.security.ldap.server.UnboundIdContainer; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.config.ldap.EmbeddedLdapServerContextSourceFactoryBean; +import org.springframework.security.config.ldap.LdapBindAuthenticationManagerFactory; import org.springframework.security.ldap.userdetails.PersonContextMapper; /** @@ -36,30 +33,19 @@ import org.springframework.security.ldap.userdetails.PersonContextMapper; public class SecurityConfig { @Bean - UnboundIdContainer ldapContainer() { - UnboundIdContainer container = new UnboundIdContainer("dc=springframework,dc=org", "classpath:users.ldif"); - container.setPort(0); - return container; + public EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean() { + EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean = EmbeddedLdapServerContextSourceFactoryBean + .fromEmbeddedLdapServer(); + contextSourceFactoryBean.setPort(0); + return contextSourceFactoryBean; } @Bean - ContextSource contextSource(UnboundIdContainer container) { - int port = container.getPort(); - return new DefaultSpringSecurityContextSource("ldap://localhost:" + port + "/dc=springframework,dc=org"); - } - - @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; + AuthenticationManager authenticationManager(BaseLdapPathContextSource contextSource) { + LdapBindAuthenticationManagerFactory factory = new LdapBindAuthenticationManagerFactory(contextSource); + factory.setUserDnPatterns("uid={0},ou=people"); + factory.setUserDetailsContextMapper(new PersonContextMapper()); + return factory.createAuthenticationManager(); } }