shiro 源码浅析

本文简要介绍了Shiro这一轻量级Java安全框架,涉及其session管理、用户认证、权限校验等功能。通过对源码的分析,重点讲解了过滤器链的核心作用,以及认证和授权的基本流程,包括自定义Realm、RememberMe机制和读写锁的应用。文中还提及了如何自定义FormAuthenticationFilter,并给出了关键代码示例。

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

 

1简介 :

Shiro是用JAVA 写轻量级权限认证安全框架 ; 拥有对 session 封装缓存,记住 cookie

用户安全密码加密认证,权限校验等特性。

2 我对源码的见解:

PS :   流程概述   据我了解核心是FilterChain (过滤器链)

因为没有合适的DEMO  所以我用

也就是shiro 源码自带的DEMO 和自带的DEMO  代码演示一遍流程,虽然生产环境一般不会直接硬编码写死配置,但也会很好的帮助我们理解源码的执行流程。

先看下他的类

 

分别代码如下

@Configuration
@SpringBootApplication
public class CliApp { //NOPMD

    public static void main(String[] args) {

        ConfigurableApplicationContext context = SpringApplication.run(CliApp.class, args);
        
        // Grab the 'QuickStart' bean, call 'run()' to start the example.
         context.getBean(QuickStart.class).run();//快速执行
    }

    /**
     * Example hard coded Realm bean.
     * @return hard coded Realm bean
     */
    @Bean
public Realm realm() {
、、//自定义realm(域)一般都是自己定义,她这里也是自己定义
        TextConfigurationRealm realm = new TextConfigurationRealm();
        realm.setUserDefinitions("joe.coder=password,user\n" +
                                 "jill.coder=password,admin");

        realm.setRoleDefinitions("admin=read,write\n" +
                                 "user=read");
        realm.setCachingEnabled(true);
        return realm;
    }

@Component
public class QuickStart {

    private static Logger log = LoggerFactory.getLogger(QuickStart.class);

    @Autowired
    private SecurityManager securityManager;

    @Autowired
    private SimpleService simpleService;

     
    
    public void run() {

        // get the current subject
        Subject subject = SecurityUtils.getSubject();//subject  相当于每一个请求的主体 ,无论是机器还是请求对象,或是移动端请求对象,一般抽象成认证个体即可

        // Subject is not authenticated yet
        Assert.isTrue(!subject.isAuthenticated());//这里硬编码,一般配置都是过滤器也就是配置authenicationFilter就可以解决具体代码可以自己去看 


        // login the subject with a username / password
        UsernamePasswordToken token = new UsernamePasswordToken("joe.coder", "password");//这里是封装用户名密码的TOKEN ,
        subject.login(token); 、、//这里是核心代码配置过滤器也是核心代码拉!!!!!


        // joe.coder has the "user" role
        subject.checkRole("user");

        // joe.coder does NOT have the admin role
        Assert.isTrue(!subject.hasRole("admin")); //跟着这个实例走都能看懂

        // joe.coder has the "read" permission
        subject.checkPermission("read");//这里检查用户权限底层代码是去调了SimpleAccount的权限VAL
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值