shiro自定义密码加密验证

现在数据库密码基本上没有明文存储的,基本上都是加过密之后的信息,今天我们来处理一下我们平时在整合shiro的时候如何处理密文的情况,shiro的登录认证的时候会获取到UsernamePasswordToken里面的password和你返回SimpleAuthenticationInfo(user, user.getPassword(), “”);里面传入的密码进行匹配,一致则通过验证。
那么我们如何自定义密码验证呢,下面我们以MD5加密作为实现

@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		UsernamePasswordToken userToken = (UsernamePasswordToken)token;
		String username = userToken.getUsername();
		SysUserEntity user = loginService.findUserByName(username);
		//将登陆用户存入session中
		Subject currentSubject = SecurityUtils.getSubject();
		Session session = currentSubject.getSession();
	    session.setAttribute("loginUser",user);
		return new SimpleAuthenticationInfo(user, user.getPassword(), "");
	}

这边很明显的是前台传过来的是明文,我返回的时候是加过密的,所以肯定不能通过验证,现在开始让shiro走我们自己的验证逻辑

@Component
public class CredentialsMatcher extends SimpleCredentialsMatcher{

	 @Override
	    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
	        UsernamePasswordToken authcToken = (UsernamePasswordToken) token;
	        Object tokenCredentials = MD5Util.md5Encrypt32Lower(String.valueOf(authcToken.getPassword())+"LJW");
	        Object accountCredentials = getCredentials(info);
	        return accountCredentials.equals(tokenCredentials);
	    }
}

使用该方法

@PostConstruct
    public void myCredentialsMatcher() {
        //该句作用是重写shiro的密码验证,让shiro用我自己的验证
        setCredentialsMatcher(credentialsMatcher);
    }

这样既可完成用户的认证