上一篇文章分享了shiro的授权与注解式开发。本篇文章将要分享shiro的会话管理以及缓存管理。
Shiro独立的会话管理,包含了单点登录的业务场景;
1.1会话监听器
- package com.zhw.shiro;
-
- import org.apache.shiro.session.Session;
- import org.apache.shiro.session.SessionListener;
-
- /**
- * @author louis
- * @create 2022-08-27 10:54
- */
- public class ShiroSessionListener implements SessionListener {
- @Override
- public void onStart(Session session) {
- System.out.println("ShiroSessionListener.onStart..."+session.getId());
- }
-
- @Override
- public void onStop(Session session) {
- System.out.println("ShiroSessionListener.onStop..."+session.getId());
- }
-
- @Override
- public void onExpiration(Session session) {
- System.out.println("ShiroSessionListener.onExpiration..."+session.getId());
- }
- }
1.2、配置applicationContext-Shiro.xml 文件
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
-
-
-
- <bean id="shiroRealm" class="com.zhw.MyRealm">
- <property name="userBiz" ref="userBiz" />
-
-
-
-
- <property name="credentialsMatcher">
- <bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
-
- <property name="hashAlgorithmName" value="md5"/>
-
- <property name="hashIterations" value="1024"/>
-
- <property name="storedCredentialsHexEncoded" value="true"/>
- bean>
- property>
- bean>
-
-
- <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
- <property name="realm" ref="shiroRealm" />
- <property name="sessionManager" ref="sessionManager">property>
- bean>
-
-
- <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
-
- <property name="securityManager" ref="securityManager" />
-
- <property name="loginUrl" value="/login"/>
-
-
-
- <property name="unauthorizedUrl