Example usage for org.springframework.security.ldap.authentication.ad ActiveDirectoryLdapAuthenticationProvider authenticate

List of usage examples for org.springframework.security.ldap.authentication.ad ActiveDirectoryLdapAuthenticationProvider authenticate

Introduction

In this page you can find the example usage for org.springframework.security.ldap.authentication.ad ActiveDirectoryLdapAuthenticationProvider authenticate.

Prototype

public Authentication authenticate(Authentication authentication) throws AuthenticationException 

Source Link

Usage

From source file:org.apache.atlas.web.security.AtlasADAuthenticationProvider.java

private Authentication getADAuthentication(Authentication authentication) {
    try {//from  w  w w.j a v a 2s  .  c  o m
        String userName = authentication.getName();
        String userPassword = "";
        if (authentication.getCredentials() != null) {
            userPassword = authentication.getCredentials().toString();
        }

        ActiveDirectoryLdapAuthenticationProvider adAuthenticationProvider = new ActiveDirectoryLdapAuthenticationProvider(
                adDomain, adURL);
        adAuthenticationProvider.setConvertSubErrorCodesToExceptions(true);
        adAuthenticationProvider.setUseAuthenticationRequestCredentials(true);

        if (userName != null && userPassword != null && !userName.trim().isEmpty()
                && !userPassword.trim().isEmpty()) {
            final List<GrantedAuthority> grantedAuths = getAuthorities(userName);
            final UserDetails principal = new User(userName, userPassword, grantedAuths);
            final Authentication finalAuthentication = new UsernamePasswordAuthenticationToken(principal,
                    userPassword, grantedAuths);
            authentication = adAuthenticationProvider.authenticate(finalAuthentication);
            if (groupsFromUGI) {
                authentication = getAuthenticationWithGrantedAuthorityFromUGI(authentication);
            }
            return authentication;
        } else {
            LOG.error("AD Authentication Failed userName or userPassword is null or empty");
            return null;
        }
    } catch (Exception e) {
        LOG.error("AD Authentication Failed:", e);
        return null;
    }
}