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 ;
59
+ import org .springframework .cache .CacheManager ;
60
+ import org .springframework .cache .caffeine .CaffeineCacheManager ;
61
+ import com .github .benmanes .caffeine .cache .Caffeine ;
62
+ import java .util .concurrent .TimeUnit ;
32
63
33
64
/**
34
65
* This class is used to enable the ldap authentication based on property
@@ -64,6 +95,9 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
64
95
@ Value ("${activedirectory.connectionTimeOut:#{127000}}" )
65
96
private Integer ldapTimeOut = DEFAULT_LDAP_CONNECTION_TIMEOUT ;
66
97
98
+ @ Value ("${LdapCacheTTL}" )
99
+ private Integer LdapCacheTTL ;
100
+
67
101
// built in connection timeout value for ldap if the network issue happens
68
102
public static final Integer DEFAULT_LDAP_CONNECTION_TIMEOUT = 127000 ;
69
103
@@ -74,20 +108,54 @@ public Integer getTimeOut() {
74
108
@ Autowired
75
109
private CustomAuthenticationEntryPoint customAuthenticationEntryPoint ;
76
110
77
- @ Autowired
78
- protected void configureGlobal (AuthenticationManagerBuilder auth ) throws Exception {
111
+ @ Bean
112
+ public UserCache userCache () {
113
+ if (cacheManager ().getCache ("authenticationCache" ) == null ) {
114
+ throw new IllegalStateException ("Cache 'authenticationCache' is required but not available" );
115
+ }
116
+ return new SpringCacheBasedUserCache (cacheManager ().getCache ("authenticationCache" ));
117
+ }
118
+
119
+ @ Bean
120
+ public CacheManager cacheManager () {
121
+ CaffeineCacheManager cacheManager = new CaffeineCacheManager ();
122
+ cacheManager .setCaffeine (Caffeine .newBuilder ()
123
+ .expireAfterWrite (LdapCacheTTL , TimeUnit .MINUTES ));
124
+ return cacheManager ;
125
+ }
126
+
127
+ @ Bean
128
+ public LdapAuthoritiesPopulator ldapAuthoritiesPopulator () {
129
+ LdapContextSource contextSource = ldapContextSource ();
130
+ return new DefaultLdapAuthoritiesPopulator (contextSource , null );
131
+ }
132
+
133
+ @ Override
134
+ public void configure (AuthenticationManagerBuilder auth ) throws Exception {
79
135
final String jasyptKey = RabbitMqPropertiesConfig .readJasyptKeyFile (jasyptKeyFilePath );
80
136
if (managerPassword .startsWith ("{ENC(" ) && managerPassword .endsWith ("}" )) {
81
137
managerPassword = DecryptionUtils .decryptString (
82
138
managerPassword .substring (1 , managerPassword .length () - 1 ), jasyptKey );
83
139
}
84
140
LOGGER .debug ("LDAP server url: " + ldapUrl );
85
- auth .ldapAuthentication ()
86
- .userSearchFilter (userSearchFilter )
87
- .contextSource (ldapContextSource ());
141
+ LdapContextSource contextSource = ldapContextSource ();
142
+ BindAuthenticator bindAuthenticator = new BindAuthenticator (contextSource );
143
+ bindAuthenticator .setUserSearch (new FilterBasedLdapUserSearch ("" , userSearchFilter , contextSource ));
144
+
145
+
146
+ LdapAuthoritiesPopulator ldapAuthoritiesPopulator = ldapAuthoritiesPopulator ();
147
+
148
+ // Create and use the caching LDAP authentication provider
149
+ CachingLdapAuthenticationProvider cachingProvider =
150
+ new CachingLdapAuthenticationProvider (bindAuthenticator , ldapAuthoritiesPopulator );
151
+
152
+ cachingProvider .setUserCache (userCache ());
153
+ auth .authenticationProvider (cachingProvider );
154
+
88
155
}
89
156
90
- public BaseLdapPathContextSource ldapContextSource () {
157
+ @ Bean
158
+ public LdapContextSource ldapContextSource () {
91
159
LdapContextSource ldap = new LdapContextSource ();
92
160
ldap .setUrl (ldapUrl );
93
161
ldap .setBase (rootDn );
0 commit comments