Shiro(三):Shiro 密码的比对、MD5盐值加密

Shiro(二)介绍如何实现一个简单的Shiro认证流程,接下来对 密码的比对与加密进行简单的介绍。

Shiro 密码的比对、MD5盐值加密

密码的比对、MD5盐值加密都是通过CrendentialsMatcher(凭证匹配器)来实现的。

一、Shiro认证时的密码比对

在Shiro进行密码比对时,一定会去拿UsernamePasswordToken 和SimpleAuthenticationInfo中封装的密码信息,那么此时要调用UsernamePasswordToken的 getPassword方法,或者调用SimpleAuthenticationInfo的getCredentials方法。

1.在UserNamePasswordkToken 中的 getPassword() 方法中打上断点,往前跟踪一下即可。
在这里插入图片描述
2.开启debug模式,点击登录,跟踪发现
SimpleCredentialsMatcher类有一个doCredentialsMatch方法,在该方法中
就进行了密码比对工作:
在这里插入图片描述

    /**
     * This implementation acquires the {@code token}'s credentials
     * (via {@link #getCredentials(AuthenticationToken) getCredentials(token)})
     * and then the {@code account}'s credentials
     * (via {@link #getCredentials(org.apache.shiro.authc.AuthenticationInfo) getCredentials(account)}) and then passes both of
     * them to the {@link #equals(Object,Object) equals(tokenCredentials, accountCredentials)} method for equality
     * comparison.
     *
     * @param token the {@code AuthenticationToken} submitted during the authentication attempt.
     * @param info  the {@code AuthenticationInfo} stored in the system matching the token principal.
     * @return {@code true} if the provided token credentials are equal to the stored account credentials,
     *         {@code false} otherwise
     */
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
   
        Object tokenCredentials = getCredentials(token);
        Object accountCredentials = getCredentials(info);
        return equals(tokenCredentials, accountCredentials);
    }

3.继续跟踪,发现我们自定义Realm的父类方法调用了 CrendebtialsMatcher组件 进行密码比对

    /**
     * Asserts that the submitted {@code AuthenticationToken}'s credentials match the stored account
     * {@code AuthenticationInfo}'s credentials, and if not, throws an {@link AuthenticationException}.
     *
     * @param token the submitted authentication token
     * @param info  the AuthenticationInfo corresponding to the given {@code token}
     * @throws AuthenticationException if the token's credentials do not match the stored account credentials.
     */
    protected void assertCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) throws AuthenticationException {
   
        CredentialsMatcher cm = getCredentialsMatcher();
        if (cm != null) {
   
            if (!cm.doCredentialsMatch(token, info)) {
   
                //not successful - throw an exception to indicate this:
                String msg = "Submitted credentials for token [" + token + "] did not match the expected credentials.";
                throw new IncorrectCredentialsException(msg);
            }
        } else {
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值