package com.skymobi.qc.admin.security;
import com.novell.ldap.LDAPException;
import com.skymobi.qc.admin.framework.ldap.LDAPSky;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import org.apache.log4j.Logger;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
public class MyAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider
{
private static Logger log = Logger.getLogger(MyAuthenticationProvider.class);
protected void additionalAuthenticationChecks(UserDetails arg0, UsernamePasswordAuthenticationToken arg1)
throws AuthenticationException
{
}
protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken arg1)
throws AuthenticationException
{
String displayName = null;
try {
displayName = LDAPSky.login(username, arg1.getCredentials().toString());
} catch (UnsupportedEncodingException localUnsupportedEncodingException) {
}
catch (LDAPException localLDAPException) {
}
if ((displayName != null) && (!displayName.equals(""))) {
Collection auths = new ArrayList();
auths.add(new GrantedAuthorityImpl("ROLE_USER"));
log.info("登录成功:" + username);
SkyUser user = new SkyUser(username, arg1.getCredentials().toString(),
true, true, true, true, auths);
user.setDispalyName(displayName);
return user;
}
log.info("登录失败:" + username);
throw new UsernameNotFoundException(username + "登录失败");
}
}