Spring Boot 整合Shiro(二)加密登录与密码加盐处理

该项目是根据上篇《Spring Boot 整合Shiro(一)登录认证和授权(附源码)》进行配置,若有不明白的请先查看上篇文章。

1.加密工具

用户注册时的密码用这个加密保存数据库


 
 
  1. /**
  2. * 账户密码加密
  3. * @param username
  4. * @param pwd
  5. * @return
  6. */
  7. public static String MD5Pwd(String username, String pwd) {
  8. String hashAlgorithmName = "MD5"; //加密方式
  9. Object crdentials = pwd; //密码原值
  10. ByteSource salt = ByteSource.Util.bytes(username); //以账号作为盐值
  11. int hashIterations = 1024; //加密1024次
  12. return new SimpleHash(hashAlgorithmName,crdentials,salt,hashIterations).toString();
  13. }

2.Shiro配置

2.1修改ShiroConfig

添加以下方法:


 
 
  1. /**
  2. * 加密配置
  3. * @return
  4. */
  5. @Bean(name = "credentialsMatcher")
  6. public HashedCredentialsMatcher hashedCredentialsMatcher() {
  7. HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
  8. // 散列算法:这里使用MD5算法;
  9. hashedCredentialsMatcher.setHashAlgorithmName( "md5");
  10. // 散列的次数,比如散列两次,相当于 md5(md5(""));
  11. hashedCredentialsMatcher.setHashIterations( 1024);
  12. // storedCredentialsHexEncoded默认是true,此时用的是密码加密用的是Hex编码;false时用Base64编码
  13. hashedCredentialsMatcher.setStoredCredentialsHexEncoded( true);
  14. return hashedCredentialsMatcher;
  15. }

修改customRealm:


 
 
  1. @Bean
  2. public CustomRealm customRealm() {
  3. CustomRealm customRealm = new CustomRealm();
  4. // 告诉realm,使用credentialsMatcher加密算法类来验证密文
  5. customRealm.setCredentialsMatcher(hashedCredentialsMatcher());
  6. customRealm.setCachingEnabled( false);
  7. return customRealm;
  8. }

2.2修改CustomRealm

当中“password”参数值是用户注册时使用加密工具生产保存的密码,可在CustomRealm中配置UserService。


 
 
  1. /**
  2. * 身份认证
  3. * 这里可以注入userService,为了方便演示直接写死账户和密码
  4. * 获取即将需要认证的信息
  5. * @param authenticationToken
  6. * @return
  7. * @throws AuthenticationException
  8. */
  9. @Override
  10. protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
  11. System.out.println( "-------身份认证方法--------");
  12. String userName = (String) authenticationToken.getPrincipal();
  13. // String userPwd = new String((char[]) authenticationToken.getCredentials());
  14. //根据用户名从数据库获取密码
  15. String password = "89267a06ce552c28e3edc11be28e4f80"; // 使用账户和明文密码:123加密后
  16. // if (userName == null) {
  17. // throw new AccountException("用户名不正确");
  18. // } else if (!userPwd.equals(password )) {
  19. // throw new AccountException("密码不正确");
  20. // }
  21. //交给AuthenticatingRealm使用CredentialsMatcher进行密码匹配
  22. ByteSource salt = ByteSource.Util.bytes(userName);
  23. return new SimpleAuthenticationInfo(userName, password, salt, getName());
  24. }

3.实战演练

使用账户:aa和密码:123 进行测试,可以看到提示登录成功。

 

之前的密码是根据账户和密码进行处理的,要注意的是注册的加密方式和设置的加密方式还有Realm中身份认证的方式都是要一模一样的

 

下篇介绍《Spring Boot 整合 Shiro(三)Kaptcha验证码》

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值