Ruoyi管理系统的自定义任务会话验证调度器实现思路

在Ruoyi中SpringSessionValidationScheduler类通过实现SessionValidationScheduler接口来实现会话验证
在这里插入图片描述
SessionValidationScheduler接口中有三个方法:
boolean isEnabled();标志会话验证是否开启
void enableSessionValidation(); 启用会话验证
void disableSessionValidation();禁用会话验证
单纯的通过接口来看我们看不出来什么,我们可以找到SessionValidationScheduler接口的实现类ExecutorServiceSessionValidationScheduler类,这个类还继承了Runnable接口
(后面我们将会说为什么要继承Runnable接口)
我们可以观察ExecutorServiceSessionValidationScheduler类中enableSessionValidation()方法
在这里插入图片描述
newSingleThreadScheduledExecutor方法返回一个ScheduledExecutorService类型的实例
在这里插入图片描述
ScheduledExecutorService类型的实例中的scheduleAtFixedRate方法意思大概就是以固定时间调度参数一中的线程。
但是在ExecutorServiceSessionValidationScheduler.enableSessionValidation方法中scheduleAtFixedRate方法参数一为this也就是ExecutorServiceSessionValidationScheduler类本身的对象,因为我刚才说了ExecutorServiceSessionValidationScheduler实现了Runnable接口所以这个方法会以固定时间调度ExecutorServiceSessionValidationScheduler中的run方法
ExecutorServiceSessionValidationScheduler.enableSessionValidation方法最后将enabled = true表示验证开启。i在这里插入图片描述
在ExecutorServiceSessionValidationScheduler.run方法中调用了sessionManager.validateSessions方法idateSessions()方法来验证会话ExecutorServiceSessionValidation.disableSessionValidation()方法用来停止线程的调度,并将enabled = tfalse表示验证关闭。这时回头看看Ruoyi中的验证会话实现类也就一目了然了。还有最后一点在ExecutorServiceSessionValidationScheduler类中 sessionManager的类型是ValidatingSessionManager,ValidatingSessionManager接口的实现类为在这里插入图片描述
其中就有DefaultWebSessionManager在Ruoyi中OnlineWebSessionManager 继承了DefaultWebSessionManager
在这里插入图片描述
并重写了validateSessions方法所以在验证会话中调用的是OnlineWebSessionManager类中的validateSessions方法
在这里插入图片描述
Ruoyi管理系统的自定义任务会话验证调度器实现思路就是这样。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> 02 <property name="securityManager" ref="securityManager"/> 03 <!-- override these for application-specific URLs if you like:--> 04 <!-- <property name="loginUrl" value="/welcome" />--> 05 <!-- <property name="successUrl" value="/blog/drug/list"/>--> 06 <property name="unauthorizedUrl" value="/unauthorized.jsp"/> 07 <!-- The 'filters' property is not necessary since any declared javax.servlet.Filter bean --> 08 <!-- defined will be automatically acquired and available via its beanName in chain --> 09 <!-- definitions, but you can perform instance overrides or name aliases here if you like: --> 10 <!-- <property name="filters"> 11 <util:map> 12 <entry key="anAlias" value-ref="someFilter"/> 13 </util:map> 14 </property> --> 15 <property name="filterChainDefinitions"> 16 <value> 17 /login* = anon 18 /welcome = anon 19 /slave4j/** = anon 20 /list = anon 21 /** = authc 22 </value> 23 </property> 24 </bean> 25 26 <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> 27 28 <!-- 基于ehCache来缓存用户认证信息和授权信息的实现 --> 29 <property name="cacheManager" ref="shiroCacheManager"/> 30 31 <!-- Single realm app. If you have multiple realms, use the 'realms' property instead. --> 32 <property name="realm" ref="jdbcRealm"/> 33 34 <!-- sessionMode参数设置为native时,那么shrio就将用户的基本认证信息保存到缺省名称为shiro-activeSessionCache 的Cache中 --> 35 <property name="sessionMode" value="native" /> 36 37 <!-- 缺省使用的是DefaultWebSessionManager来管理Session,该管理类缺省使用的是使用MemorySessionDAO基于内存来保存和操作用户基本认证信息。如果系统内的用户数特别多,我们需要使用CacheSessionDao来基于Cache进行操作 --> 38 <property name="sessionManager" ref="shiroSessionManager"/> 39 </bean> 40 41 <!-- jdbcRealm --> 42 <bean id="jdbcRealm" class="org.slave4j.shiro.MyRealm"> 43 <!-- shiro.authorizationCache是用来存放授权信息的Cache --> 44 <property name="authorizationCacheName" value="shiro.authorizationCache"/> 45 </bean> 46 47 <!-- shiroSessionManager --> 48 <bean id="shiroSessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager"> 49 <property name="sessionDAO" ref="sessionDAO"/> 50 <property name="sessionListeners"> 51 <set><bean class="org.slave4j.shiro.SessionHandler"/> </set> 52 </property> 53 54 <property name="sessionValidationScheduler" ref="sessionValidationScheduler" /> 55 <property name="globalSessionTimeout" value="180000"/> 56 <property name="sessionValidationSchedulerEnabled" value="true"/> 57 <property name="deleteInvalidSessions" value="true"/> 58 </bean> 59 <bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO"> 60 <property name="activeSessionsCacheName" value="shiro-activeSessionCache"/> 61 </bean> 62 <bean id="sessionValidationScheduler" class="org.apache.shiro.session.mgt.ExecutorServiceSessionValidationScheduler"> 63 <property name="interval" value="5000"/> 64 </bean> 65 66 <!-- shiroCacheManager --> 67 <!-- Let's use some enterprise caching support for better performance. You can replace this with any enterprise 68 caching framework implementation that you like (Terracotta+Ehcache, Coherence, GigaSpaces, etc --> 69 <bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> 70 <!-- Set a net.sf.ehcache.CacheManager instance here if you already have one. If not, a new one 71 will be created with a default config: --> 72 <property name="cacheManager" ref="ehCacheManager"/> 73 <!-- If you don't have a pre-built net.sf.ehcache.CacheManager instance to inject, but you want 74 a specific Ehcache configuration to be used, specify that here. If you don't, a default 75 will be used.:--> 76 <property name="cacheManagerConfigFile" value="classpath:shiro_ehcache.xml"/> 77 </bean> 78 <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"/> 79 80 <!-- Enable Shiro Annotations for Spring-configured beans. Only run after the lifecycleBeanProcessor has run: --> 81 <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> 82 <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> 83 <property name="securityManager" ref="securityManager"/> 84 </bean>

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值