|
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.cache.annotation.Cacheable; |
| 33 | +import org.springframework.beans.factory.annotation.Autowired; |
| 34 | +import org.springframework.context.annotation.Configuration; |
| 35 | +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; |
| 36 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 37 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 38 | + |
| 39 | +import org.springframework.beans.factory.annotation.Autowired; |
| 40 | +import org.springframework.beans.factory.annotation.Value; |
| 41 | +import org.springframework.cache.concurrent.ConcurrentMapCache; |
| 42 | +import org.springframework.context.annotation.Bean; |
| 43 | +import org.springframework.context.annotation.Configuration; |
| 44 | +import org.springframework.ldap.core.support.BaseLdapPathContextSource; |
| 45 | +import org.springframework.ldap.core.support.LdapContextSource; |
| 46 | +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; |
| 47 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 48 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 49 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 50 | +import org.springframework.security.core.userdetails.UserCache; |
| 51 | +import org.springframework.security.core.userdetails.cache.SpringCacheBasedUserCache; |
| 52 | +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
| 53 | +import org.springframework.security.crypto.password.PasswordEncoder; |
| 54 | +import org.springframework.security.ldap.authentication.BindAuthenticator; |
| 55 | +import org.springframework.security.ldap.authentication.LdapAuthenticator; |
| 56 | +import org.springframework.security.ldap.search.FilterBasedLdapUserSearch; |
| 57 | +import org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator; |
| 58 | +import org.springframework.security.ldap.userdetails.LdapAuthoritiesPopulator; |
32 | 59 |
|
33 | 60 | /**
|
34 | 61 | * This class is used to enable the ldap authentication based on property
|
@@ -74,20 +101,44 @@ public Integer getTimeOut() {
|
74 | 101 | @Autowired
|
75 | 102 | private CustomAuthenticationEntryPoint customAuthenticationEntryPoint;
|
76 | 103 |
|
77 |
| - @Autowired |
78 |
| - protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { |
| 104 | + @Bean |
| 105 | + public UserCache userCache() { |
| 106 | + // Adjust cache settings as necessary |
| 107 | + return new SpringCacheBasedUserCache(new ConcurrentMapCache("authenticationCache")); |
| 108 | + } |
| 109 | + |
| 110 | + @Bean |
| 111 | + public LdapAuthoritiesPopulator ldapAuthoritiesPopulator() { |
| 112 | + return new DefaultLdapAuthoritiesPopulator(ldapContextSource(), null); // Adjust the second parameter based on your group search base |
| 113 | + // Additional configuration can be set here if necessary |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + @Override |
| 118 | + public void configure(AuthenticationManagerBuilder auth) throws Exception { |
79 | 119 | final String jasyptKey = RabbitMqPropertiesConfig.readJasyptKeyFile(jasyptKeyFilePath);
|
80 | 120 | if (managerPassword.startsWith("{ENC(") && managerPassword.endsWith("}")) {
|
81 | 121 | managerPassword = DecryptionUtils.decryptString(
|
82 | 122 | managerPassword.substring(1, managerPassword.length() - 1), jasyptKey);
|
83 | 123 | }
|
84 | 124 | LOGGER.debug("LDAP server url: " + ldapUrl);
|
85 |
| - auth.ldapAuthentication() |
86 |
| - .userSearchFilter(userSearchFilter) |
87 |
| - .contextSource(ldapContextSource()); |
| 125 | + |
| 126 | + BindAuthenticator bindAuthenticator = new BindAuthenticator(ldapContextSource()); |
| 127 | + bindAuthenticator.setUserSearch(new FilterBasedLdapUserSearch("", userSearchFilter, ldapContextSource())); |
| 128 | + |
| 129 | + |
| 130 | + LdapAuthoritiesPopulator ldapAuthoritiesPopulator = ldapAuthoritiesPopulator(); |
| 131 | + |
| 132 | + // Create and use the caching LDAP authentication provider |
| 133 | + CachingLdapAuthenticationProvider cachingProvider = |
| 134 | + new CachingLdapAuthenticationProvider(bindAuthenticator, ldapAuthoritiesPopulator); |
| 135 | + |
| 136 | + cachingProvider.setUserCache(userCache()); |
| 137 | + auth.authenticationProvider(cachingProvider); |
| 138 | + |
88 | 139 | }
|
89 | 140 |
|
90 |
| - public BaseLdapPathContextSource ldapContextSource() { |
| 141 | + public LdapContextSource ldapContextSource() { |
91 | 142 | LdapContextSource ldap = new LdapContextSource();
|
92 | 143 | ldap.setUrl(ldapUrl);
|
93 | 144 | ldap.setBase(rootDn);
|
|
0 commit comments