|
21 | 21 | import org.springframework.beans.factory.annotation.Autowired;
|
22 | 22 | import org.springframework.beans.factory.annotation.Value;
|
23 | 23 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
| 24 | +import org.springframework.context.annotation.Bean; |
24 | 25 | import org.springframework.context.annotation.Configuration;
|
25 | 26 | import org.springframework.context.annotation.Profile;
|
26 |
| -import org.springframework.ldap.core.support.BaseLdapPathContextSource; |
27 | 27 | import org.springframework.ldap.core.support.LdapContextSource;
|
28 | 28 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
29 | 29 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
30 | 30 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
31 | 31 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
| 32 | +import org.springframework.security.ldap.authentication.BindAuthenticator; |
| 33 | +import org.springframework.security.ldap.authentication.LdapAuthenticationProvider; |
| 34 | +import org.springframework.security.ldap.authentication.LdapAuthenticator; |
| 35 | +import org.springframework.security.ldap.search.FilterBasedLdapUserSearch; |
| 36 | +import org.springframework.ldap.pool.validation.DefaultDirContextValidator; |
| 37 | +import org.springframework.ldap.core.ContextSource; |
32 | 38 |
|
33 | 39 | /**
|
34 | 40 | * This class is used to enable the ldap authentication based on property
|
@@ -74,20 +80,33 @@ public Integer getTimeOut() {
|
74 | 80 | @Autowired
|
75 | 81 | private CustomAuthenticationEntryPoint customAuthenticationEntryPoint;
|
76 | 82 |
|
77 |
| - @Autowired |
78 |
| - protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { |
| 83 | + @Override |
| 84 | + public void configure(AuthenticationManagerBuilder auth) throws Exception { |
79 | 85 | final String jasyptKey = RabbitMqPropertiesConfig.readJasyptKeyFile(jasyptKeyFilePath);
|
80 | 86 | if (managerPassword.startsWith("{ENC(") && managerPassword.endsWith("}")) {
|
81 | 87 | managerPassword = DecryptionUtils.decryptString(
|
82 | 88 | managerPassword.substring(1, managerPassword.length() - 1), jasyptKey);
|
83 | 89 | }
|
84 | 90 | LOGGER.debug("LDAP server url: " + ldapUrl);
|
85 |
| - auth.ldapAuthentication() |
86 |
| - .userSearchFilter(userSearchFilter) |
87 |
| - .contextSource(ldapContextSource()); |
| 91 | + |
| 92 | + // Initialize and configure the LdapContextSource |
| 93 | + LdapContextSource contextSource = ldapContextSource(); |
| 94 | + |
| 95 | + // Configure BindAuthenticator with the context source and user search filter |
| 96 | + BindAuthenticator bindAuthenticator = new BindAuthenticator(contextSource); |
| 97 | + bindAuthenticator.setUserSearch(new FilterBasedLdapUserSearch( |
| 98 | + "", // Empty base indicates search starts at root DN provided in contextSource |
| 99 | + userSearchFilter, |
| 100 | + contextSource)); |
| 101 | + |
| 102 | + // Setup LdapAuthenticationProvider |
| 103 | + LdapAuthenticationProvider ldapAuthProvider = new LdapAuthenticationProvider(bindAuthenticator); |
| 104 | + |
| 105 | + // Configure the authentication provider |
| 106 | + auth.authenticationProvider(ldapAuthProvider); |
88 | 107 | }
|
89 | 108 |
|
90 |
| - public BaseLdapPathContextSource ldapContextSource() { |
| 109 | + public LdapContextSource ldapContextSource() { |
91 | 110 | LdapContextSource ldap = new LdapContextSource();
|
92 | 111 | ldap.setUrl(ldapUrl);
|
93 | 112 | ldap.setBase(rootDn);
|
|
0 commit comments