日期:2014-05-16 浏览次数:20692 次
package com.cas;
import org.inspektr.common.ioc.annotation.NotNull;
import org.jasig.cas.adaptors.jdbc.AbstractJdbcUsernamePasswordAuthenticationHandler;
import org.jasig.cas.authentication.handler.AuthenticationException;
import org.jasig.cas.authentication.handler.BadPasswordAuthenticationException;
import org.jasig.cas.authentication.handler.UnknownUsernameAuthenticationException;
import org.jasig.cas.authentication.principal.UsernamePasswordCredentials;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* Class that if provided a query that returns a password (parameter of query must be username) will compare that
* password to a translated version of the password provided by the user. If they match, then authentication succeeds.
* Default password translator is plaintext translator.
*
* @Date 2009-5-23
*/
public class JdbcUsernamePasswordAuthHandlerImpl extends AbstractJdbcUsernamePasswordAuthenticationHandler {
// it's better to move below properties to external configure file, for example 'maxFailureTimes'
private static final String QUERY_USER_SQL = "select * from user_info where username = ?";
private static final String FAILURE_TRIGGER_SQL = "update user_info set failureTimes = ? where username = ?";
private static final String LOCK_USER_SQL = "update user_info set failureTimes = ?, isValid = ? where username = ?";
@NotNull
private String maxFailureTimes;
/**
* @param paraMaxFailureTimes
* the maxFailureTimes to set
*/
public void setMaxFailureTimes(String paraMaxFailureTimes) {
this.maxFailureTimes = paraMaxFailureTimes;
}
/**
* authenticate username password internal
*
* @param credentials
* credentials
* @throws AuthenticationException
* AuthenticationException
* @return true if user login success
* @see org.jasig.cas.authentication.handler.support.AbstractUsernamePasswordAuthenticationHandler
* #authenticateUsernamePasswordInternal(org.jasig.cas.authentication.principal.UsernamePasswordCredentials)
*/
@Override
protected boolean authenticateUsernamePasswordInternal(final UsernamePasswordCredentials credentials)
throws AuthenticationException {
final String username = credentials.getUsername();
final String password = credentials.getPassword();
JdbcTemplate template = new JdbcTemplate(getDataSource());
try {
// get user info by username, if no result found, auto throw IncorrectResultSizeDataAccessException
UserInfo userInfo = (UserInfo) template.queryForObject(QUERY_USER_SQL, new String[]{username},
new BeanPropertyRowMapper(UserInfo.class));
// check user lock
if (!"Y".equalsIgnoreCase(userInfo.getIsValid())) {
// means user was locked
throw new AccountLockedException();
} else if (password.equals(userInfo.getPassword())) {
// means correct username/password, login success return true