import org.jasig.cas.Exception.LDCAuthenticationException;
import org.jasig.cas.authentication.handler.support.AbstractUsernamePasswordAuthenticationHandler;
import org.jasig.cas.authentication.principal.UsernamePasswordCredentials;
import org.springframework.util.StringUtils;
public class LDCUserAuthenticationHandler extends AbstractUsernamePasswordAuthenticationHandler{
@Override
protected boolean authenticateUsernamePasswordInternal(UsernamePasswordCredentials credentials)
throws LDCAuthenticationException {
final String username = credentials.getUsername();
final String password = credentials.getPassword();
if (StringUtils.hasText(username) && StringUtils.hasText(password)
&& username.equals(getPasswordEncoder().encode(password))) {
log
.debug("User [" + username
+ "] was successfully authenticated.");
return true;
}
log.debug("User [" + username + "] failed authentication");
return false;
}
}
自定义异常
import org.jasig.cas.authentication.handler.AuthenticationException;
public class LDCAuthenticationException extends AuthenticationException {
private static final long serialVersionUID = 3906648604830611762L;
/**
* 本构造方法可以返回任何自定义的异常消息到前台
* <p>例子:TongXiangAuthenticationException(new Exception("自定义的异常消息"))</p>
*/
public LDCAuthenticationException(final Throwable throwable)
{
super(throwable.getMessage());
}
/**
* 本构造方法只能返回在messages_zh_CN.properties中已经定义好的异常消息
*/
public LDCAuthenticationException(final String code)
{
super(code);
}
}