Skip to content

Commit b441b53

Browse files
author
Piyush Sadangi (EXT)
committed
EIFA:369: Reduce 6 LDAP calls to 4
1 parent a36b905 commit b441b53

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

publish-common/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@
6262
<artifactId>spring-security-ldap</artifactId>
6363
<scope>compile</scope>
6464
</dependency>
65+
<dependency>
66+
<groupId>org.springframework.ldap</groupId>
67+
<artifactId>spring-ldap-core</artifactId>
68+
<version>2.3.8.RELEASE</version>
69+
</dependency>
6570
<dependency>
6671
<groupId>io.springfox</groupId>
6772
<artifactId>springfox-swagger2</artifactId>

publish-service/src/main/java/com/ericsson/eiffel/remrem/publish/config/SecurityConfig.java

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.beans.factory.annotation.Autowired;
2222
import org.springframework.beans.factory.annotation.Value;
2323
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
24+
import org.springframework.context.annotation.Bean;
2425
import org.springframework.context.annotation.Configuration;
2526
import org.springframework.context.annotation.Profile;
2627
import org.springframework.ldap.core.support.BaseLdapPathContextSource;
@@ -29,6 +30,16 @@
2930
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
3031
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
3132
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
33+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
34+
import org.springframework.security.crypto.password.PasswordEncoder;
35+
import org.springframework.security.ldap.authentication.BindAuthenticator;
36+
import org.springframework.security.ldap.authentication.LdapAuthenticationProvider;
37+
import org.springframework.security.ldap.authentication.LdapAuthenticator;
38+
import org.springframework.security.ldap.search.FilterBasedLdapUserSearch;
39+
import org.springframework.ldap.pool.validation.DefaultDirContextValidator;
40+
import org.springframework.ldap.pool.factory.PoolingContextSource;
41+
import org.springframework.ldap.core.ContextSource;
42+
import org.springframework.ldap.core.support.BaseLdapPathContextSource;
3243

3344
/**
3445
* This class is used to enable the ldap authentication based on property
@@ -74,20 +85,33 @@ public Integer getTimeOut() {
7485
@Autowired
7586
private CustomAuthenticationEntryPoint customAuthenticationEntryPoint;
7687

77-
@Autowired
78-
protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
88+
@Override
89+
public void configure(AuthenticationManagerBuilder auth) throws Exception {
7990
final String jasyptKey = RabbitMqPropertiesConfig.readJasyptKeyFile(jasyptKeyFilePath);
8091
if (managerPassword.startsWith("{ENC(") && managerPassword.endsWith("}")) {
8192
managerPassword = DecryptionUtils.decryptString(
8293
managerPassword.substring(1, managerPassword.length() - 1), jasyptKey);
8394
}
8495
LOGGER.debug("LDAP server url: " + ldapUrl);
85-
auth.ldapAuthentication()
86-
.userSearchFilter(userSearchFilter)
87-
.contextSource(ldapContextSource());
96+
97+
// Initialize and configure the LdapContextSource
98+
LdapContextSource contextSource = ldapContextSource();
99+
100+
// Configure BindAuthenticator with the context source and user search filter
101+
BindAuthenticator bindAuthenticator = new BindAuthenticator(contextSource);
102+
bindAuthenticator.setUserSearch(new FilterBasedLdapUserSearch(
103+
"", // Empty base indicates search starts at root DN provided in contextSource
104+
userSearchFilter,
105+
contextSource));
106+
107+
// Setup LdapAuthenticationProvider
108+
LdapAuthenticationProvider ldapAuthProvider = new LdapAuthenticationProvider(bindAuthenticator);
109+
110+
// Configure the authentication provider
111+
auth.authenticationProvider(ldapAuthProvider);
88112
}
89113

90-
public BaseLdapPathContextSource ldapContextSource() {
114+
public LdapContextSource ldapContextSource() {
91115
LdapContextSource ldap = new LdapContextSource();
92116
ldap.setUrl(ldapUrl);
93117
ldap.setBase(rootDn);

0 commit comments

Comments
 (0)