shiro + MD5的登录验证

1. 在shiro的配置类ShiroConfiguration中添加凭证匹配器

  /**
   * 凭证匹配器
   * 密码校验规则HashedCredentialsMatcher
   * 这个类是为了对密码进行编码的 ,
   * 防止密码在数据库里明码保存 , 当然在登陆认证的时候 ,
   * 这个类也负责对form里输入的密码进行编码
   * 处理认证匹配处理器:如果自定义需要实现继承HashedCredentialsMatcher
   * @return
   */
  @Bean(name="hashedCredentialsMatcher")
  public HashedCredentialsMatcher hashedCredentialsMatcher(){
    HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();

    hashedCredentialsMatcher.setHashAlgorithmName("MD5");//散列算法:这里使用MD5算法;
    hashedCredentialsMatcher.setHashIterations(3);//散列的次数
    //storedCredentialsHexEncoded默认是true,此时用的是密码加密用的是Hex编码;false时用Base64编码
    hashedCredentialsMatcher.setStoredCredentialsHexEncoded(true);

    return hashedCredentialsMatcher;
  }

2. 在shiro的配置类ShiroConfiguration中,将凭证匹配器加入到realm域中

  @Bean(name = "realm")
  @ConditionalOnMissingBean(name = "realm")
  public Realm realm() {
    Class<?> realmClass = shiroProperties.getRealmClass();
    if (realmClass != null) {
      AuthorizingRealm realm = (AuthorizingRealm) BeanUtils.instantiate(realmClass);
      realm.setCredentialsMatcher(hashedCredentialsMatcher());//向realm域中注入凭证匹配器
      log.debug("Common-shiro-ShiroConfiguration-realm  设置完毕");
      return realm;
    }
    return null;
  }

3. 修改web服务中的realm类中的认证方法

        添加加密时的盐,这里的盐是一个固定值。最好使用用户的用户名之类的字符串来做盐,这样可以使相同的密码产生不同的加密字符串。

//盐值
ByteSource credentialsSalt  = ByteSource.Util.bytes("sys_user");
return new SimpleAuthenticationInfo(principal, user.getPassword(), credentialsSalt, getName());

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值