一般我们都使用查询数据库用户表方式处理认证。
如图,deployerConfigContext.xml配置文件中配置:
我们是用反编译工具可以看到QueryDatabaseAuthenticationHandler这个接口的具体实现,如下直接贴出源码:
package org.jasig.cas.adaptors.jdbc;
import java.security.GeneralSecurityException;
import javax.security.auth.login.AccountNotFoundException;
import javax.security.auth.login.FailedLoginException;
import javax.validation.constraints.NotNull;
import org.jasig.cas.authentication.HandlerResult;
import org.jasig.cas.authentication.PreventedException;
import org.jasig.cas.authentication.UsernamePasswordCredential;
import org.jasig.cas.authentication.handler.PasswordEncoder;
import org.jasig.cas.authentication.principal.SimplePrincipal;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
public class QueryDatabaseAuthenticationHandler extends AbstractJdbcUsernamePasswordAuthenticationHandler
{
@NotNull
private String sql;
protected final HandlerResult authenticateUsernamePasswordInternal(UsernamePasswordCredential credential)
throws GeneralSecurityException, PreventedException
{
String username = credential.getUsername();
String encryptedPassword = getPasswordEncoder().encode(credential.getPassword());
try {
String dbPassword = (String)getJdbcTemplate().queryForObject(this.sql, String.class, new Object[] { username });
if (!dbPassword.equals(encryptedPassword))
throw new FailedLoginException("Password does not match value on record.");
}
catch (IncorrectResultSizeDataAccessException e) {
if (e.getActualSize() == 0) {
throw new AccountNotFoundException(username + " not found with SQL query");
}
throw new FailedLoginException("Multiple records found for " + username);
}
catch (DataAccessException e) {
throw new PreventedException("SQL exception while executing query for " + username, e);
}
return createHandlerResult(credential, new SimplePrincipal(username), null);
}
public void setSql(String sql)
{
this.sql = sql;
}
}
我们知道认证处理器,做用户验证的时候会执行这个方法:authenticateUsernamePasswordInternal
如上图,可以看到第二步骤,sql的查询条件只有一个username,如果需要使用邮箱、手机号码、用户代码等进行认证,那么可以修改这部分代码。
为保证源jar的完整性,我们自己用maven工具创建一个jar来修改它,如下:
如上图,根据需求用户既可以根据用户名,又可以根据用户代码进行认证,则新增一个usercode,然后maven打成jar,我们就可以使用该jar了。
并修改deployerConfigContext.xml配置文件,如下:
文章转载于: https://blog.csdn.net/lkx94/article/details/42916973
版权声明:博客对我来说是记忆的笔记和知识的分享~非常感谢博客大神的帮助,若有无意侵权,请您联系我,谢谢 https://blog.csdn.net/qq_36698956