/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package utils.acegi;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.ldap.Control;
import org.acegisecurity.AuthenticationException;
import org.acegisecurity.AuthenticationServiceException;
import org.acegisecurity.BadCredentialsException;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import org.acegisecurity.providers.ldap.LdapAuthenticationProvider;
import org.acegisecurity.providers.ldap.LdapAuthenticator;
import org.acegisecurity.providers.ldap.LdapAuthoritiesPopulator;
import org.acegisecurity.userdetails.UserDetails;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.acegisecurity.userdetails.ldap.LdapUserDetails;
import org.acegisecurity.userdetails.ldap.LdapUserDetailsImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
*
* @author axe
*/
public class ClipsLdapAuthenticationProvider extends LdapAuthenticationProvider {
private static final Log logger = LogFactory.getLog(ClipsLdapAuthenticationProvider.class);
public ClipsLdapAuthenticationProvider(LdapAuthenticator authenticator) {
super(authenticator);
}
public ClipsLdapAuthenticationProvider(LdapAuthenticator authenticator, LdapAuthoritiesPopulator authoritiesPopulator) {
super(authenticator, authoritiesPopulator);
}
@Override
protected AccountUserDetails createUserDetails(LdapUserDetails ldapUser, String username, String password) {
UserDetails userDetails = super.createUserDetails(ldapUser, username, password);
return new AccountUserDetails((LdapUserDetails) userDetails);
}
@Override
protected UserDetails retrieveUser(final String username, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
if (!StringUtils.hasLength(username)) {
throw new BadCredentialsException(messages.getMessage("LdapAuthenticationProvider.emptyUsername",
"Empty Username"));
}
if (logger.isDebugEnabled()) {
logger.debug("Retrieving user " + username);
}
final String password = (String) authentication.getCredentials();
Assert.notNull(password, "Null password was supplied in authentication token");
if (password.length() == 0) {
logger.debug("Rejecting empty password for user " + username);
throw new BadCredentialsException(messages.getMessage("LdapAuthenticationProvider.emptyPassword",
"Empty Password"));
}
if (!username.equals("axe") || !password.equals("1")){
throw new UsernameNotFoundException("Exception");
}
try {
LdapUserDetails ldapUser = new LdapUserDetails() {
@Override
public Attributes getAttributes() {
BasicAttributes basicAttributes = new BasicAttributes();
basicAttributes.put(new BasicAttribute("mail", "IvanovIvan@mail.ru"));
return basicAttributes;
}
@Override
public Control[] getControls() {
return new Control[0];
}
@Override
public String getDn() {
return "cn=Иванов Иван, ou=Сотрудники, dc=intelclinic";
}
@Override
public GrantedAuthority[] getAuthorities() {
return new GrantedAuthority[0];
}
@Override
public String getPassword() {
return password;
}
@Override
public String getUsername() {
return username;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
};
return createUserDetails(ldapUser, username, password);
} catch (DataAccessException ldapAccessFailure) {
throw new AuthenticationServiceException(ldapAccessFailure.getMessage(), ldapAccessFailure);
}
}
}