Last active
August 29, 2015 14:22
-
-
Save efraimcf/a89beebcc76a1ca3a32c to your computer and use it in GitHub Desktop.
LDAP Authentication Provider customized to get username for every attempt
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
package br.com.project.auth; | |
import org.apache.commons.lang3.Validate; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.context.annotation.Scope; | |
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | |
import org.springframework.security.core.Authentication; | |
import org.springframework.security.core.AuthenticationException; | |
import org.springframework.security.ldap.authentication.LdapAuthenticationProvider; | |
import org.springframework.security.ldap.authentication.LdapAuthenticator; | |
import org.springframework.security.ldap.userdetails.LdapAuthoritiesPopulator; | |
import org.springframework.stereotype.Controller; | |
@Controller | |
@Scope("request") | |
public class MyCustomAuthenticationProvider extends LdapAuthenticationProvider { | |
private final Logger log = LoggerFactory | |
.getLogger(MyCustomAuthenticationProvider.class); | |
public MyCustomAuthenticationProvider(LdapAuthenticator authenticator, | |
LdapAuthoritiesPopulator authoritiesPopulator) { | |
super(authenticator, authoritiesPopulator); | |
} | |
@Override | |
public Authentication authenticate(Authentication authentication) | |
throws AuthenticationException { | |
Validate.notEmpty(authentication.getName()); | |
Validate.notNull(authentication.getCredentials()); | |
Validate.notEmpty(authentication.getCredentials().toString()); | |
String username = authentication.getName(); | |
log.info("=========================> User {} is trying to login.", username); | |
Authentication userAuthenticated = super.authenticate(authentication); | |
return userAuthenticated; | |
} | |
@Override | |
public boolean supports(Class<?> authentication) { | |
return authentication.equals(UsernamePasswordAuthenticationToken.class); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment