Skip to content

Instantly share code, notes, and snippets.

@efraimcf
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save efraimcf/a89beebcc76a1ca3a32c to your computer and use it in GitHub Desktop.
Save efraimcf/a89beebcc76a1ca3a32c to your computer and use it in GitHub Desktop.
LDAP Authentication Provider customized to get username for every attempt
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