CAS5自定义加密方法获取用户名
项目用到CAS单点登录,根据用户名自定义加密方式
1. 添加pom.xml依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
2.修改application.properties文件
cas.authn.jdbc.query[0].passwordEncoder.type=org.apereo.cas.MyPasswordEncode
#cas.authn.jdbc.query[0].passwordEncoder.encodingAlgorithm=MD5
3. 实现PasswordEncoder接口,根据上下文获取用户名
@Component
public class MyPasswordEncode implements PasswordEncoder {
@Override
public String encode(CharSequence rawPassword) {
String password = String.valueOf(rawPassword);
//通过上下文获取用户输入用户名
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
String username = String.valueOf(request.getParameter("username"));
String passwordEncode = PasswordUtil.encrypt(username, password);
return passwordEncode;
}
@Override
public boolean matches(CharSequence rawPassword, String encodedPassword) {
// 判断密码为空,直接返回false
if (StringUtils.isBlank(rawPassword)) {
return false;
}
//调用上面的encode 对请求密码进行自定义加密处理
String pass = this.encode(rawPassword.toString());
//比较密码是否相等
return pass.equals(encodedPassword);
}
标签:rawPassword,CAS5.3,加密,String,自定义,用户名,return
From: https://www.cnblogs.com/zhufanfan/p/16722272.html