|
@@ -15,10 +15,12 @@
|
|
|
*/
|
|
|
package org.springframework.security.config.annotation.authentication.ldap
|
|
|
|
|
|
+import org.springframework.beans.factory.config.AutowireCapableBeanFactory
|
|
|
import org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory;
|
|
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
|
|
import org.springframework.context.annotation.Bean
|
|
|
import org.springframework.context.annotation.Configuration
|
|
|
+import org.springframework.context.annotation.Import
|
|
|
import org.springframework.ldap.core.ContextSource;
|
|
|
import org.springframework.ldap.core.support.BaseLdapPathContextSource;
|
|
|
import org.springframework.security.authentication.AuthenticationManager
|
|
@@ -27,7 +29,12 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio
|
|
|
import org.springframework.security.config.annotation.BaseSpringSpec
|
|
|
import org.springframework.security.config.annotation.SecurityBuilder;
|
|
|
import org.springframework.security.config.annotation.authentication.AuthenticationManagerBuilder
|
|
|
-import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
|
|
+import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
|
|
|
+import org.springframework.security.config.annotation.authentication.configuration.EnableGlobalAuthentication
|
|
|
+import org.springframework.security.config.annotation.configuration.AutowireBeanFactoryObjectPostProcessor
|
|
|
+import org.springframework.security.config.annotation.configuration.AutowireBeanFactoryObjectPostProcessorTests
|
|
|
+import org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration
|
|
|
+import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder
|
|
|
import org.springframework.security.ldap.DefaultSpringSecurityContextSource;
|
|
|
import org.springframework.security.ldap.authentication.LdapAuthenticationProvider;
|
|
@@ -58,6 +65,7 @@ class LdapAuthenticationProviderBuilderSecurityBuilderTests extends BaseSpringSp
|
|
|
auth
|
|
|
.ldapAuthentication()
|
|
|
.contextSource(contextSource())
|
|
|
+ .userDnPatterns("uid={0},ou=people")
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -75,6 +83,7 @@ class LdapAuthenticationProviderBuilderSecurityBuilderTests extends BaseSpringSp
|
|
|
auth
|
|
|
.ldapAuthentication()
|
|
|
.contextSource(contextSource())
|
|
|
+ .userDnPatterns("uid={0},ou=people")
|
|
|
.groupRoleAttribute("group")
|
|
|
}
|
|
|
}
|
|
@@ -93,6 +102,7 @@ class LdapAuthenticationProviderBuilderSecurityBuilderTests extends BaseSpringSp
|
|
|
auth
|
|
|
.ldapAuthentication()
|
|
|
.contextSource(contextSource())
|
|
|
+ .userDnPatterns("uid={0},ou=people")
|
|
|
.groupSearchFilter("ou=groupName");
|
|
|
}
|
|
|
}
|
|
@@ -111,6 +121,7 @@ class LdapAuthenticationProviderBuilderSecurityBuilderTests extends BaseSpringSp
|
|
|
auth
|
|
|
.ldapAuthentication()
|
|
|
.contextSource(contextSource())
|
|
|
+ .userDnPatterns("uid={0},ou=people")
|
|
|
.rolePrefix("role_")
|
|
|
}
|
|
|
}
|
|
@@ -121,7 +132,7 @@ class LdapAuthenticationProviderBuilderSecurityBuilderTests extends BaseSpringSp
|
|
|
AuthenticationManager auth = context.getBean(AuthenticationManager)
|
|
|
then:
|
|
|
auth
|
|
|
- auth.authenticate(new UsernamePasswordAuthenticationToken("admin","password")).authorities.collect { it.authority }.sort() == ["ROLE_ADMIN","ROLE_USER"]
|
|
|
+ auth.authenticate(new UsernamePasswordAuthenticationToken("bob","bobspassword")).authorities.collect { it.authority }.sort() == ["ROLE_DEVELOPERS"]
|
|
|
}
|
|
|
|
|
|
@Configuration
|
|
@@ -131,30 +142,29 @@ class LdapAuthenticationProviderBuilderSecurityBuilderTests extends BaseSpringSp
|
|
|
.ldapAuthentication()
|
|
|
.contextSource(contextSource())
|
|
|
.groupSearchBase("ou=groups")
|
|
|
+ .groupSearchFilter("(member={0})")
|
|
|
.userDnPatterns("uid={0},ou=people");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
def "SEC-2472: Can use crypto PasswordEncoder"() {
|
|
|
setup:
|
|
|
- PasswordEncoderConfig.PE = Mock(PasswordEncoder)
|
|
|
loadConfig(PasswordEncoderConfig)
|
|
|
when:
|
|
|
AuthenticationManager auth = context.getBean(AuthenticationManager)
|
|
|
then:
|
|
|
- auth.authenticate(new UsernamePasswordAuthenticationToken("admin","password")).authorities.collect { it.authority }.sort() == ["ROLE_ADMIN","ROLE_USER"]
|
|
|
- PasswordEncoderConfig.PE.matches(_, _) << true
|
|
|
+ auth.authenticate(new UsernamePasswordAuthenticationToken("bcrypt","password")).authorities.collect { it.authority }.sort() == ["ROLE_DEVELOPERS"]
|
|
|
}
|
|
|
|
|
|
@Configuration
|
|
|
static class PasswordEncoderConfig extends BaseLdapServerConfig {
|
|
|
- static PasswordEncoder PE
|
|
|
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
|
|
auth
|
|
|
.ldapAuthentication()
|
|
|
.contextSource(contextSource())
|
|
|
- .passwordEncoder(PE)
|
|
|
+ .passwordEncoder(new BCryptPasswordEncoder())
|
|
|
.groupSearchBase("ou=groups")
|
|
|
+ .groupSearchFilter("(member={0})")
|
|
|
.userDnPatterns("uid={0},ou=people");
|
|
|
}
|
|
|
}
|
|
@@ -167,23 +177,44 @@ class LdapAuthenticationProviderBuilderSecurityBuilderTests extends BaseSpringSp
|
|
|
static abstract class BaseLdapServerConfig extends BaseLdapProviderConfig {
|
|
|
@Bean
|
|
|
public ApacheDSContainer ldapServer() throws Exception {
|
|
|
- ApacheDSContainer apacheDSContainer = new ApacheDSContainer("dc=springframework,dc=org", "classpath:/users.ldif");
|
|
|
- apacheDSContainer.setPort(33389);
|
|
|
+ ApacheDSContainer apacheDSContainer = new ApacheDSContainer("dc=springframework,dc=org", "classpath:/test-server.ldif");
|
|
|
+ apacheDSContainer.setPort(getPort());
|
|
|
return apacheDSContainer;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Configuration
|
|
|
+ @EnableGlobalAuthentication
|
|
|
+ @Import(ObjectPostProcessorConfiguration)
|
|
|
static abstract class BaseLdapProviderConfig {
|
|
|
|
|
|
@Bean
|
|
|
public BaseLdapPathContextSource contextSource() throws Exception {
|
|
|
DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource(
|
|
|
- "ldap://127.0.0.1:33389/dc=springframework,dc=org")
|
|
|
+ "ldap://127.0.0.1:"+ getPort() + "/dc=springframework,dc=org")
|
|
|
contextSource.userDn = "uid=admin,ou=system"
|
|
|
contextSource.password = "secret"
|
|
|
- contextSource.afterPropertiesSet();
|
|
|
+ contextSource.afterPropertiesSet()
|
|
|
return contextSource;
|
|
|
}
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public AuthenticationManager authenticationManager(AuthenticationManagerBuilder auth) {
|
|
|
+ configure(auth)
|
|
|
+ auth.build()
|
|
|
+ }
|
|
|
+
|
|
|
+ abstract protected void configure(AuthenticationManagerBuilder auth)
|
|
|
+ }
|
|
|
+
|
|
|
+ static Integer port;
|
|
|
+
|
|
|
+ static int getPort() {
|
|
|
+ if(port == null) {
|
|
|
+ ServerSocket socket = new ServerSocket(0)
|
|
|
+ port = socket.localPort
|
|
|
+ socket.close()
|
|
|
+ }
|
|
|
+ port
|
|
|
}
|
|
|
}
|