forked from eiffel-community/eiffel-remrem-publish
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Piyush Sadangi (EXT)
committed
Mar 25, 2024
1 parent
b6f644f
commit a7f0ae9
Showing
3 changed files
with
175 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...ain/java/com/ericsson/eiffel/remrem/publish/config/CachingLdapAuthenticationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.ericsson.eiffel.remrem.publish.config; | ||
|
||
import org.springframework.cache.concurrent.ConcurrentMapCache; | ||
import org.springframework.security.authentication.BadCredentialsException; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.userdetails.UserCache; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.cache.NullUserCache; | ||
import org.springframework.security.core.userdetails.cache.SpringCacheBasedUserCache; | ||
import org.springframework.security.ldap.authentication.LdapAuthenticationProvider; | ||
import org.springframework.security.ldap.authentication.LdapAuthenticator; | ||
import org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator; | ||
import org.springframework.security.ldap.userdetails.LdapAuthoritiesPopulator; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.StringUtils; | ||
|
||
public class CachingLdapAuthenticationProvider extends LdapAuthenticationProvider { | ||
|
||
private UserCache userCache = new NullUserCache(); | ||
|
||
/** | ||
* Create an instance with the supplied authenticator and authorities populator | ||
* implementations. | ||
* | ||
* @param authenticator the authentication strategy (bind, password comparison, etc) | ||
* to be used by this provider for authenticating users. | ||
* @param authoritiesPopulator the strategy for obtaining the authorities for a given | ||
*/ | ||
|
||
public CachingLdapAuthenticationProvider(LdapAuthenticator authenticator, LdapAuthoritiesPopulator authoritiesPopulator) { | ||
super(authenticator, authoritiesPopulator); | ||
} | ||
|
||
public void setUserCache(UserCache userCache) { | ||
this.userCache = userCache; | ||
} | ||
|
||
|
||
@Override | ||
public Authentication authenticate(Authentication authentication) { | ||
String userName = authentication.getName(); | ||
UsernamePasswordAuthenticationToken userToken = (UsernamePasswordAuthenticationToken) authentication; | ||
UserDetails userDetailsFromCache = userCache.getUserFromCache(userName); | ||
if (userDetailsFromCache != null) { | ||
additionalAuthenticationChecks(userDetailsFromCache, userToken); | ||
return createSuccessfulAuthentication(userToken, userDetailsFromCache); | ||
} else { | ||
Authentication authenticationFromProvider = super.authenticate(authentication); | ||
userCache.putUserInCache((UserDetails)authenticationFromProvider.getPrincipal()); | ||
return authenticationFromProvider; | ||
} | ||
|
||
} | ||
|
||
protected void additionalAuthenticationChecks(UserDetails userDetails, | ||
UsernamePasswordAuthenticationToken authentication) { | ||
if (StringUtils.isEmpty(authentication.getCredentials())) { | ||
|
||
throw new BadCredentialsException(messages.getMessage( | ||
"AbstractUserDetailsAuthenticationProvider.badCredentials", | ||
"Bad credentials")); | ||
} | ||
String presentedPassword = authentication.getCredentials().toString(); | ||
if (!StringUtils.isEmpty(userDetails.getPassword()) && (!presentedPassword.equals(userDetails.getPassword()))) { | ||
System.out.println("Authentication failed: password does not match stored value"); | ||
throw new BadCredentialsException(messages.getMessage( | ||
"AbstractUserDetailsAuthenticationProvider.badCredentials", | ||
"Bad credentials")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters