基于SSM的RBAC权限系统(4)-巧用Shiro自带加密
背景
在打算用Shiro做密码加密的时候,一开始Shiro只能在验证登录口令时Shiro才拿加密密码以及自己从数据库的密码去匹配,但是在注册、添加、编辑需要再次加密的时候却找不到对应的办法,难道要我自己写个方法加密?但是既然Shiro有对登录密码进行加密,那么肯定就有加密的地方,跟着调试器debug进去,果然发现了这些方法。
然后,问题就解决了。
ByteSource credentialsSalt = ByteSource.Util.bytes(record.getAccount());
SimpleHash simpleHash = new SimpleHash("MD5", record.getPassword(), credentialsSalt, 16);
record.setPassword(simpleHash.toString());
附上认证的配置
<bean id="jdbcRealm" class="cn.etop.rbac.common.shiro.realms.ShiroRealm">
<property name="credentialsMatcher">
<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<property name="hashAlgorithmName" value="MD5"></property>
<property name="hashIterations" value="16"></property>
</bean>
</property>
</bean>
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
UsernamePasswordToken upToken = (UsernamePasswordToken) authenticationToken;
String username = upToken.getUsername();
boolean isExit=false;
try {
isExit = userService.checkUserNameExit(username);
} catch (Exception e) {
e.printStackTrace();
}
if(isExit){
throw new UnknownAccountException("用户不存在!");
}
User temp=null;
try {
temp=loginServiceImpl.getUserByAccount (username);
} catch (Exception e) {
e.printStackTrace();
}
Object principal = username;
Object credentials = temp.getPassword();
String realmName = getName();
ByteSource credentialsSalt = ByteSource.Util.bytes(username);
SimpleAuthenticationInfo info = null;
info = new SimpleAuthenticationInfo(principal, credentials, credentialsSalt ,realmName);
return info;
}
完整项目地址
这是我第一个写的web项目,代码烂得飞起,仅供纪念,不做参考
带Shiro版:https://github.com/EnTaroAdunZ/ssm_rbac_shiro.git
不带Shiro版:https://github.com/EnTaroAdunZ/ssm_rbac.git