Shiro学习笔记(二):自定义Realm的实现

        上一篇文章中提到了Realm,它相当于datasource数据源,SecurityManager进行安全验证时需要通过Realm获取用户权限数据,比如:如果用户身份数据在数据库,那么Realm就需要从数据库中获取用户身份信息。因此,在本文中我们将通过跟踪源码的形式来看一下Shiro中的认证和授权是如何完成的。

1、 源码跟踪

以上一篇文章的代码为例,在subject.login()进行断点调试,整体过程如下:

(1)进入DelegatingSubject的login()方法。

  (2)调用DelegatingSubject的login()方法

  (3)调用本类中的authenticate方法

  (4)调用AbstractAuthenticator中的authenticate()方法。

(5)调用本类(AbstractAuthenticator)的 doAuthenticate方法

protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException {
        this.assertRealmsConfigured();
        Collection<Realm> realms = this.getRealms();
        return realms.size() == 1 ? this.doSingleRealmAuthentication((Realm)realms.iterator().next(), authenticationToken) : this.doMultiRealmAuthentication(realms, authenticationToken);
    }

在该方法中有两个注意点:

a.调用assertRealmsConfigured()方法读取.ini文件中的配置信息。getRealms()方法会返回Realm的个数。

 b.判断配置文件中Realm的个数:

  • 如果是一个,则调用doSingleRealmAuthentication()方法。
  • 如果有多个,则调用doMultiRealmAuthentication()方法。

        因为在shiro.ini中只配置了一个Realm,所以这里的size=1,进入doSingleRealmAuthentication()方法。

(6)在doSingleRealmAuthentication()方法中调用getAuthenticationInfo()方法。

(7)调用 getCachedAuthenticationInfo()方法,判断缓存中是否有token信息。

 (8)如果缓存中没有token信息,调用SimpleAccountRealm类的doGetAuthenticationInfo()方法

 (9)SimpleAccountRealm类的doGetAuthenticationInfo()方法实现对用户名的校验。

(10)前面步骤是对用户名的校验,而对密码的校验是在 AuthenticatingRealm类中getAuthenticationInfo()方法。

(11)调用getCredentialsMatcher()方法完成密码校验。

 整体流程如下图所示:

针对上述分析,我们可以得到以下结论:

  • 用户名校验是在SimpleAccountRealm的doGetAuthenticationInfo()这个方法完成的。
  • 密码校验是在AuthenticationRealm中的assertCredentialsMatch()方法中完成的。

总结:

  • 认证的Realm:AuthenticationgRealm,需实现的方法doGetAuthenticationInfo()
  • 授权的Realm:AuthorizingRealm,需实现的方法doGetAuthorizationInfo()

2、自定义Realm

(1)主程序

package org.example.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;

public class TestShiro {
    public static void main(String[] args) {
        //创建defaultSecurityManager
        DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
        //通过读取配置文件设置Realm
        defaultSecurityManager.setRealm(new CustomerRealm());
        //将安装工具类中设置默认安全管理器
        SecurityUtils.setSecurityManager(defaultSecurityManager);
        //获取主体对象
        Subject subject = SecurityUtils.getSubject();
        //创建token令牌
        UsernamePasswordToken token = new UsernamePasswordToken("admin", "admin");
        try{
            //用户登录
            subject.login(token);
            System.out.println("登陆成功");
        }catch (UnknownAccountException e){
            e.printStackTrace();
            System.out.println("用户名错误");
        }catch (IncorrectCredentialsException e){
            e.printStackTrace();
            System.out.println("密码错误");
        }
    }
}

(2)自定义Realm

package org.example.shiro;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class CustomerRealm extends AuthorizingRealm {
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //在token中获取用户名
        String principal = (String) authenticationToken.getPrincipal();
        System.out.println(principal);
        //这里可以从数据库中读取数据,本程序中只是模拟
        if("admin".equals(principal)){
            //参数说明:返回数据库中正确的用户名 返回数据库中的正确密码 提供当前的realm名字
            SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(principal, "admin", this.getName());
            return simpleAuthenticationInfo;
        }
        return null;
    }
}

(3)执行结果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值