Skip to content

Commit 6b56071

Browse files
committed
Add LDAP factory beans
Issue gh-10138
1 parent 2894ed3 commit 6b56071

9 files changed

+1035
-0
lines changed

config/spring-security-config.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ dependencies {
7373
testImplementation "org.apache.directory.server:apacheds-protocol-ldap"
7474
testImplementation "org.apache.directory.server:apacheds-server-jndi"
7575
testImplementation 'org.apache.directory.shared:shared-ldap'
76+
testImplementation "com.unboundid:unboundid-ldapsdk"
7677
testImplementation 'jakarta.persistence:jakarta.persistence-api'
7778
testImplementation 'org.hibernate:hibernate-core-jakarta'
7879
testImplementation 'org.hsqldb:hsqldb'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.config.ldap;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.api.extension.ExtendWith;
21+
22+
import org.springframework.beans.factory.UnsatisfiedDependencyException;
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.context.annotation.Bean;
25+
import org.springframework.ldap.core.support.LdapContextSource;
26+
import org.springframework.security.authentication.AuthenticationManager;
27+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
28+
import org.springframework.security.config.test.SpringTestContext;
29+
import org.springframework.security.config.test.SpringTestContextExtension;
30+
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
31+
import org.springframework.test.web.servlet.MockMvc;
32+
33+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
34+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin;
35+
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated;
36+
37+
@ExtendWith(SpringTestContextExtension.class)
38+
public class EmbeddedLdapServerContextSourceFactoryBeanITests {
39+
40+
public final SpringTestContext spring = new SpringTestContext(this);
41+
42+
@Autowired
43+
private MockMvc mockMvc;
44+
45+
@Test
46+
public void contextSourceFactoryBeanWhenEmbeddedServerThenAuthenticates() throws Exception {
47+
this.spring.register(FromEmbeddedLdapServerConfig.class).autowire();
48+
49+
this.mockMvc.perform(formLogin().user("bob").password("bobspassword"))
50+
.andExpect(authenticated().withUsername("bob"));
51+
}
52+
53+
@Test
54+
public void contextSourceFactoryBeanWhenPortZeroThenAuthenticates() throws Exception {
55+
this.spring.register(PortZeroConfig.class).autowire();
56+
57+
this.mockMvc.perform(formLogin().user("bob").password("bobspassword"))
58+
.andExpect(authenticated().withUsername("bob"));
59+
}
60+
61+
@Test
62+
public void contextSourceFactoryBeanWhenCustomLdifAndRootThenAuthenticates() throws Exception {
63+
this.spring.register(CustomLdifAndRootConfig.class).autowire();
64+
65+
this.mockMvc.perform(formLogin().user("pg").password("password")).andExpect(authenticated().withUsername("pg"));
66+
}
67+
68+
@Test
69+
public void contextSourceFactoryBeanWhenCustomManagerDnThenAuthenticates() throws Exception {
70+
this.spring.register(CustomManagerDnConfig.class).autowire();
71+
72+
this.mockMvc.perform(formLogin().user("bob").password("bobspassword"))
73+
.andExpect(authenticated().withUsername("bob"));
74+
}
75+
76+
@Test
77+
public void contextSourceFactoryBeanWhenManagerDnAndNoPasswordThenException() {
78+
assertThatExceptionOfType(UnsatisfiedDependencyException.class)
79+
.isThrownBy(() -> this.spring.register(CustomManagerDnNoPasswordConfig.class).autowire())
80+
.withRootCauseInstanceOf(IllegalStateException.class)
81+
.withMessageContaining("managerPassword is required if managerDn is supplied");
82+
}
83+
84+
@EnableWebSecurity
85+
static class FromEmbeddedLdapServerConfig {
86+
87+
@Bean
88+
EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean() {
89+
return EmbeddedLdapServerContextSourceFactoryBean.fromEmbeddedLdapServer();
90+
}
91+
92+
@Bean
93+
AuthenticationManager authenticationManager(LdapContextSource contextSource) {
94+
LdapBindAuthenticationManagerFactory factory = new LdapBindAuthenticationManagerFactory(contextSource);
95+
factory.setUserDnPatterns("uid={0},ou=people");
96+
return factory.createAuthenticationManager();
97+
}
98+
99+
}
100+
101+
@EnableWebSecurity
102+
static class PortZeroConfig {
103+
104+
@Bean
105+
EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean() {
106+
EmbeddedLdapServerContextSourceFactoryBean factoryBean = EmbeddedLdapServerContextSourceFactoryBean
107+
.fromEmbeddedLdapServer();
108+
factoryBean.setPort(0);
109+
return factoryBean;
110+
}
111+
112+
@Bean
113+
AuthenticationManager authenticationManager(LdapContextSource contextSource) {
114+
LdapBindAuthenticationManagerFactory factory = new LdapBindAuthenticationManagerFactory(contextSource);
115+
factory.setUserDnPatterns("uid={0},ou=people");
116+
return factory.createAuthenticationManager();
117+
}
118+
119+
}
120+
121+
@EnableWebSecurity
122+
static class CustomLdifAndRootConfig {
123+
124+
@Bean
125+
EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean() {
126+
EmbeddedLdapServerContextSourceFactoryBean factoryBean = EmbeddedLdapServerContextSourceFactoryBean
127+
.fromEmbeddedLdapServer();
128+
factoryBean.setLdif("classpath*:test-server2.xldif");
129+
factoryBean.setRoot("dc=monkeymachine,dc=co,dc=uk");
130+
return factoryBean;
131+
}
132+
133+
@Bean
134+
AuthenticationManager authenticationManager(LdapContextSource contextSource) {
135+
LdapBindAuthenticationManagerFactory factory = new LdapBindAuthenticationManagerFactory(contextSource);
136+
factory.setUserDnPatterns("uid={0},ou=gorillas");
137+
return factory.createAuthenticationManager();
138+
}
139+
140+
}
141+
142+
@EnableWebSecurity
143+
static class CustomManagerDnConfig {
144+
145+
@Bean
146+
EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean() {
147+
EmbeddedLdapServerContextSourceFactoryBean factoryBean = EmbeddedLdapServerContextSourceFactoryBean
148+
.fromEmbeddedLdapServer();
149+
factoryBean.setManagerDn("uid=admin,ou=system");
150+
factoryBean.setManagerPassword("secret");
151+
return factoryBean;
152+
}
153+
154+
@Bean
155+
AuthenticationManager authenticationManager(LdapContextSource contextSource) {
156+
LdapPasswordComparisonAuthenticationManagerFactory factory = new LdapPasswordComparisonAuthenticationManagerFactory(
157+
contextSource, NoOpPasswordEncoder.getInstance());
158+
factory.setUserDnPatterns("uid={0},ou=people");
159+
return factory.createAuthenticationManager();
160+
}
161+
162+
}
163+
164+
@EnableWebSecurity
165+
static class CustomManagerDnNoPasswordConfig {
166+
167+
@Bean
168+
EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean() {
169+
EmbeddedLdapServerContextSourceFactoryBean factoryBean = EmbeddedLdapServerContextSourceFactoryBean
170+
.fromEmbeddedLdapServer();
171+
factoryBean.setManagerDn("uid=admin,ou=system");
172+
return factoryBean;
173+
}
174+
175+
@Bean
176+
AuthenticationManager authenticationManager(LdapContextSource contextSource) {
177+
LdapPasswordComparisonAuthenticationManagerFactory factory = new LdapPasswordComparisonAuthenticationManagerFactory(
178+
contextSource, NoOpPasswordEncoder.getInstance());
179+
factory.setUserDnPatterns("uid={0},ou=people");
180+
return factory.createAuthenticationManager();
181+
}
182+
183+
}
184+
185+
}

0 commit comments

Comments
 (0)