shiro认证基本过程

本文详细介绍了使用Shiro框架进行用户认证的基本步骤,包括获取Subject、判断用户认证状态、封装用户名密码、执行登录操作及自定义Realm方法等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

认证/授权基本步骤
     ①获取当前的 Subject,调用SecurityUtils.getSubject();
     ②判断当前用户是否已经认证,调用 Subject 的 isAuthenticated();
     ③或没有被认证,则把用户名和密码封装为 UserNamePassworkToken 对象
          [1]客户端提交的表单数据。
          [2]提交数据到SpringMVC的 handler 上。
          [3]获取用户名和密码。
     ④执行登录,调用 Subject 的 login(AuthencationToken);
     ⑤自定义Realm 方法,从数据库中获用户安全数据,

     1.使用token的用户名去数据库中查询数据返回(SimpleAuthenticationInfo)给Shiro

          [1]实际上需要继承 org.apache.shiro.realm.AuthencatingRealm 类(仅仅实现认证,org.apache.shiro.realm.AuthorizingRealm 可以用来认证和授权)。
          [2]实现 doGetAuthenticationInfo(AuthenticationToken) 方法。
     ⑥最后由Shiro完成密码的比对。

密码的匹配 
最终使用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)登录成功。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值