shiro的HashedCredentialsMatcher密码匹配过程

1.加密
用户注册时,系统为输入的密码进行加密,此处使用MD5算法,“密码+盐(用户名+随机数)”的方式生成散列值:

 

public class passwordEncry{

 

    //随机数生成器

    private static RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();

 

    //指定散列算法为md5

    private String algorithmName = "MD5";

    //散列迭代次数

    private final int hashIterations = 2;

 

    /**

     * 生成随机盐值对密码进行加密

     * @param userLogin  登录识别串(用户名)

     * @return

     */

    public UserLogin encrypt(UserLogin userLogin) {

        userLogin.setSalt(randomNumberGenerator.nextBytes().

        toHex());

        String newPassword =

        new SimpleHash(algorithmName,userLogin.getPassword(),

            ByteSource.Util.bytes(userLogin.getCredentialsSalt()),hashIterations).toHex();

 

        userLogin.setPassword(newPassword);

        return userLogin;

 

        ``````````````````````

    }

    }

这里的userLogin.getCredentialsSalt()为加密盐,我这里的盐设为(登录识别串+随机数),生成一个新的加密密码,并把新密码覆盖原明文密码存到数据库。加密完成。

2.HashedCredentialsMatcher的配置

<bean id="credentialsMatcher"

        class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">

        <property name="hashAlgorithmName" value="MD5" />

        <property name="hashIterations" value="2" />

        <property name="storedCredentialsHexEncoded" value="true" />

    </bean>

HashedCredentialsMatcher配置的属性值要跟加密时的属性(hashAlgorithmName,hashIterations,storedCredentialsHexEncoded)一致,storedCredentialsHexEnc表示是否存储散列后的密码为16进制,需要和生成密码时的一样。

3.得到Token
从客户输入获取token(令牌)

   @Override

    protected AuthenticationToken createToken(ServletRequest request,ServletResponse response) {

        String username = request.getParameter("loginString");

        String password = request.getParameter("pwd");

        String rememberMe = request.getParameter("rememberMe");

        String host = getHost(request);

        UsernamePasswordToken token =

                new UsernamePasswordToken(username,

                password,

                Boolean.parseBoolean(rememberMe), host);

 

                return token;

    }

4.Realm认证
自己实现一个Realm,重写doGetAuthenticationInfo,得到一个AuthenticationInfo对象。

 @Override

    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

 

        String username = (String)token.getPrincipal();

 

        UserLogin userLogin = userService.getUserLogin(username);

 

        if (userLogin == null) {

            throw new UnknownAccountException("用户不存在");

        }

 

        if("S".equals(userLogin.getStatus())) {

            throw new LockedAccountException("无效账号");

        }

 

        SimpleAuthenticationInfo authenticationInfo =

        new SimpleAuthenticationInfo(

            userLogin, //登录识别串信息

            userLogin.getPassword(), //密码

           ByteSource.Util.bytes(userLogin.getCredentialsSalt()),//盐值

                getName()  //realm name

        );

        return authenticationInfo;

    }

5.密码的匹配
最终会来到HashedCredentialsMatcher类的doCredentialsMatch()方法进行密码的比对,此方法包含两个参数AuthenticationToken token, AuthenticationInfo info,思路一下子豁然开朗。

  @Override

    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {

 

        Object tokenHashedCredentials =

        hashProvidedCredentials(token, info); 

 

        Object accountCredentials = getCredentials(info);

 

       return equals(tokenHashedCredentials, accountCredentials);

    }

方法的最后通过一个equals函数进行散列的比较:

           //equals()方法的主要代码:

            byte[] tokenBytes = toBytes(tokenCredentials);

            byte[] accountBytes = toBytes(accountCredentials);

            return Arrays.equals(tokenBytes, accountBytes);

验证完毕,最后执行subject.login(token)登录成功。

此处只简单介绍了shiro一种匹配器HashedCredentialsMatcher的认证过程,除了这个匹配器还有另外的匹配器,如PasswordMatcher(密码匹配器),道理都差不多

 

转载于:https://my.oschina.net/kangJonney/blog/1531000

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值