Skip to content

Commit df5f5f4

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

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

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

Lines changed: 29 additions & 7 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,12 @@
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;
3239

3340
/**
3441
* This class is used to enable the ldap authentication based on property
@@ -74,20 +81,34 @@ public Integer getTimeOut() {
7481
@Autowired
7582
private CustomAuthenticationEntryPoint customAuthenticationEntryPoint;
7683

77-
@Autowired
78-
protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
84+
@Override
85+
public void configure(AuthenticationManagerBuilder auth) throws Exception {
7986
final String jasyptKey = RabbitMqPropertiesConfig.readJasyptKeyFile(jasyptKeyFilePath);
8087
if (managerPassword.startsWith("{ENC(") && managerPassword.endsWith("}")) {
8188
managerPassword = DecryptionUtils.decryptString(
8289
managerPassword.substring(1, managerPassword.length() - 1), jasyptKey);
8390
}
84-
LOGGER.debug("LDAP server url: " + ldapUrl);
85-
auth.ldapAuthentication()
86-
.userSearchFilter(userSearchFilter)
87-
.contextSource(ldapContextSource());
91+
LOGGER.debug("LDAP server url: {}", ldapUrl);
92+
93+
// Initialize and configure the LdapContextSource
94+
LdapContextSource contextSource = ldapContextSource();
95+
96+
// Configure BindAuthenticator with the context source and user search filter
97+
BindAuthenticator bindAuthenticator = new BindAuthenticator(contextSource);
98+
bindAuthenticator.setUserSearch(new FilterBasedLdapUserSearch(
99+
"", // Empty base indicates search starts at root DN provided in contextSource
100+
userSearchFilter,
101+
contextSource));
102+
103+
// Setup LdapAuthenticationProvider
104+
LdapAuthenticationProvider ldapAuthProvider = new LdapAuthenticationProvider(bindAuthenticator);
105+
106+
// Configure the authentication provider
107+
auth.authenticationProvider(ldapAuthProvider);
88108
}
89109

90-
public BaseLdapPathContextSource ldapContextSource() {
110+
@Bean
111+
public LdapContextSource ldapContextSource() {
91112
LdapContextSource ldap = new LdapContextSource();
92113
ldap.setUrl(ldapUrl);
93114
ldap.setBase(rootDn);
@@ -100,6 +121,7 @@ public BaseLdapPathContextSource ldapContextSource() {
100121
return ldap;
101122
}
102123

124+
103125
@Override
104126
protected void configure(HttpSecurity http) throws Exception {
105127
LOGGER.debug("LDAP authentication enabled");

0 commit comments

Comments
 (0)