|
@@ -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.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;
|
|
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() {
|
|
|
|
- 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
|
|
@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();
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|