Shiro -认证凭据(密码)加密的那些事

简介: Shiro -认证凭据(密码)加密的那些事

一般来说,实际项目中隐私数据没有在网络上明文跑路,都会采用不同的加密算法。Shiro中的认证凭据通常也会采用算法进行加密。

【1】CredentialsMatcher接口


该接口只有一个方法,doCredentialsMatch,就是用来进行密码比较的!

源码如下:

public interface CredentialsMatcher {
    /**
     * Returns {@code true} if the provided token credentials match the stored account credentials,
     * {@code false} otherwise.
     *
     * @param token   the {@code AuthenticationToken} submitted during the authentication attempt
     * @param info the {@code AuthenticationInfo} stored in the system.
     * @return {@code true} if the provided token credentials match the stored account credentials,
     *         {@code false} otherwise.
     */
    boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info);
}

其实现类如下:

里面可以看到我们常用的MD5算法和SHA-X算法。


其中Md5CredentialsMatcher 和Sha1CredentialsMatcher 标注已经过时,通常我们会直接在容器中注册HashedCredentialsMatcher来使用!


如这里我们注册HashedCredentialsMatcher并指定算法为Md5,xml配置如下:

<!-- 自定义Realm -->
<bean id="customRealm" class="com.web.maven.shiro.CustomRealm">
   <!-- 将凭证匹配器设置到realm中,realm按照凭证匹配器的要求进行散列 -->
   <property name="credentialsMatcher">
      <bean  class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
    <!-- 加密算法 -->
         <property name="hashAlgorithmName" value="MD5"/>
         <!-- 加密次数 -->
         <property name="hashIterations" value="1"/>
       </bean>
   </property>
</bean>



【2】盐值加密

使用【1】中的配置情况下,如果两个人的原始密码一样,那么其加密后的密码也相同,同样存在风险。


在HashedCredentialsMatcher中有这样一个方法:

 protected Hash hashProvidedCredentials(Object credentials, Object salt, int hashIterations) {
     String hashAlgorithmName = assertHashAlgorithmName();
     return new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations);
 }


其中new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations)就是根据提供的原始凭据,加密算法,盐值和加密次数进行加密并返回加密后的结果。盐值是一个唯一字符串,这里你可以使用loginName作为加密盐值。


步骤如下:


在 doGetAuthenticationInfo 方法返回值创建 SimpleAuthenticationInfo 对象的时候, 需要使用

SimpleAuthenticationInfo(principal, credentials, credentialsSalt, realmName) 构造器;


使用 ByteSource.Util.bytes() 来计算盐值;


盐值需要唯一, 一般使用随机字符串或 user id;


使用 new SimpleHash(hashAlgorithmName, credentials, salt, hashIterations); 来计算盐值加密后的密码的值。


如果使用盐值加密,我们的doGetAuthenticationInfo修改如下:

  @Override
  protected AuthenticationInfo doGetAuthenticationInfo(
      AuthenticationToken authenticationToken) throws AuthenticationException {
      UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
          //获取页面传来的用户账号
      String loginName = token.getUsername();
          //根据登录账号从数据库查询用户信息
          SysUser user = sysUserService.getUserByLoginCode(loginName);
          System.out.println("从数据库查询到的用户信息 : "+user);
          //一些异常新娘西
          if (null == user) {
            throw new UnknownAccountException();//没找到帐号
          }
          if (user.getStatus()==null||user.getStatus()==0) {
            throw new LockedAccountException();//帐号被锁定
          }
          //其他异常...
          //返回AuthenticationInfo的实现类SimpleAuthenticationInfo
//          return new SimpleAuthenticationInfo(user, user.getPassword(), this.getName());
          //盐值加密
          ByteSource credentialsSalt = ByteSource.Util.bytes(loginName);
          return new SimpleAuthenticationInfo(user, user.getPassword(), credentialsSalt, this.getName());
  }


目录
相关文章
|
4月前
|
移动开发 JavaScript 安全
js的常见的三种密码加密方式-MD5加密、Base64加密和解密和sha1加密详解总结
js的常见的三种密码加密方式-MD5加密、Base64加密和解密和sha1加密详解总结
115 0
|
15天前
|
数据安全/隐私保护 C++
【C++】凯撒密码 实现加密与解密
【C++】凯撒密码 实现加密与解密
|
2月前
|
编解码 Java Nacos
nacos常见问题之密码加密配置如何解决
Nacos是阿里云开源的服务发现和配置管理平台,用于构建动态微服务应用架构;本汇总针对Nacos在实际应用中用户常遇到的问题进行了归纳和解答,旨在帮助开发者和运维人员高效解决使用Nacos时的各类疑难杂症。
151 0
|
2月前
|
存储 运维 Nacos
nacos常见问题之连接用户名和密码把明文用户名和密码进行加密如何解决
Nacos是阿里云开源的服务发现和配置管理平台,用于构建动态微服务应用架构;本汇总针对Nacos在实际应用中用户常遇到的问题进行了归纳和解答,旨在帮助开发者和运维人员高效解决使用Nacos时的各类疑难杂症。
214 2
|
2月前
|
存储 缓存 安全
https跳过SSL认证时是不是就是不加密的,相当于http?
https跳过SSL认证时是不是就是不加密的,相当于http?
123 0
|
2月前
|
应用服务中间件 Nacos 数据安全/隐私保护
nacos常见问题之如何用明文密码做加密
Nacos是阿里云开源的服务发现和配置管理平台,用于构建动态微服务应用架构;本汇总针对Nacos在实际应用中用户常遇到的问题进行了归纳和解答,旨在帮助开发者和运维人员高效解决使用Nacos时的各类疑难杂症。
129 0
|
2月前
|
存储 API 数据库
微搭低代码密码加密存储
微搭低代码密码加密存储
|
3月前
|
安全 搜索推荐 API
【现代密码学】笔记 补充7-- CCA安全与认证加密《introduction to modern cryphtography》
【现代密码学】笔记 补充7-- CCA安全与认证加密《introduction to modern cryphtography》
110 0
|
4月前
|
安全 算法 Java
spring security 如何对密码进行加密
spring security 如何对密码进行加密