Shiro-Session
概述
Shiro提供了完整的企业级会话管理功能,不依赖于底层容器(如web容器tomcat),不管JavaSE还是JavaEE环境都可以使用,提供了会话管理、会话事件监听、会话存储/持久化、容器无关的集群、失效/过期支持、对Web 的透明支持、SSO 单点登录的支持等特性。
会话相关的API


会话监听器
会话监听器用于监听会话创建、过期及停止事件

添加Session,这里添加的时候使用的是 HttpSession
@RequestMapping("/shiroMethod")
public String shiroServiceMethod(HttpSession session){
session.setAttribute("key", "value12345");
shiroService.shiroServiceMethod();
return "redirect:/list.jsp";
}
在Service层中获取Session,注意这里使用的是Shiro提供的Session
package com.java.shiro.services; import java.util.Date; import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.session.Session; public class ShiroService { @RequiresRoles({ "admin" })
public void shiroServiceMethod() { Session session = SecurityUtils.getSubject().getSession();
System.out.println("session: " + session.getAttribute("key")); System.out.println("Test ShiroServiceMethod, time: " + new Date());
} }
这样的好处是,即便是在Service层也能访问到Session的数据,开发的时候是很方便的,这是Shiro提供的Session一个很重要的应用
SessionDao
可以把Session存到数据库中,对session进行增删改查操作。

在开发中推荐 继承 EnterpreiseCacheSessionDAO
思路:

配置示例:


我们把配置好的sessionManager 作为SecurityManager的属性进行配置。
数据表:
create table sessions (
id varchar(200),
session varchar(2000),
constraint pk_sessionsprimary key(id)
) charset=utf8 ENGINE=InnoDB;
Session Dao


在进行Session操作时,我们需要将这个Session对象进行序列化的操作
SerializableUtils

配置完成。
会话验证调度器

实际上会在底层开一个线程,验证会话是否过期了,使用会话验证会影响性能,开发是用的不多。
缓存
CacheManagerAware接口
Shiro内部相应的组件(DefaultSecurityManager) 会自动检测相应的对象(如Realm) 是否实现了CacheManagerAware并自动注入相应的CacheManager。
Realm缓存
Shiro 提供了 CachingRealm,其实实现了CacheManagerAware 接口,提供了缓存的一些基础实现;
AuthenticatingRealm 及 AuthorizingRealm也分别提供了对 AuthenticationInfo 和 AuthorizationInfo信息的缓存。
Session缓存

Shiro-Session的更多相关文章
- 修改记录-优化后(springboot+shiro+session+redis+ngnix共享)
1.普通用户实现redis共享session 1.配置 #cache指定缓存类型 spring.cache.type=REDIS #data-redis spring.redis.database=1 ...
- 解决org.apache.shiro.session.UnknownSessionException: There is no session with id的问题
一.背景 最近在整合了Spring+Shiro+Redis实现tomcat集群session共享的问题之后,发布以后运行以后发现老是会出现:org.apache.shiro.session.Unkno ...
- org.apache.shiro.session.InvalidSessionException: java.lang.IllegalStateException: getAttribute: Session already invalidated] with root cause
1.遇到以下异常,找了好长时间,终于解决,报的异常如下: 七月 07, 2017 3:02:16 下午 org.apache.catalina.core.StandardWrapperValve in ...
- Shiro session和Spring session一样吗?
出自:https://yq.aliyun.com/articles/114167?t=t1 1. 疑问 我们在项目中使用了spring mvc作为MVC框架,shiro作为权限控制框架,在使用过程中慢 ...
- nginx 、tomcat 集群配置、shiro Session 共享
一.nginx.config 配置 #user nobody; worker_processes ; #error_log logs/error.log; #error_log logs/error. ...
- shiro session管理
http://shiro.apache.org/session-management.html Using Sessions The SessionManager Session Timeout Pe ...
- org.apache.shiro.session.UnknownSessionException: There is no session with id [xxxx]的解决方案
org.apache.shiro.session.UnknownSessionException: There is no session with id [xxxx]的解决方案 背景描述 Sprin ...
- 【Shiro】六、Apache Shiro Session管理
1.Session的介绍 关于Session 会话:从启动一个Session到关闭这个Session作为一个会话,是对客户端和服务器端交互的一种封装,带有时效性 会话的产生: 一般从容器中产生 Web ...
- 集群环境下Shiro Session的管理
问题引入 紧接上篇连接 在多台tomcat集群中,shiro管理的session需要放在Redis中,我们只需要增加redisSessionDAO的配置就行 <!-- 定义会话管理器的操作 表示 ...
- shiro session过期后ajax请求跳转(转)
配置了 Shrio框架,session也集成进去了 ,发现问题session会话过期,点击页面,一直请求失败.本来想集成拦截器,过滤器,但是已经用了shiro框架,sessionDestroyed 方 ...
随机推荐
- eclipse(1)----ubuntu下的安装与配置
eclipse的安装与配置 1.eclipse官网下载,最新版本eclipse-jee-oxygen-3-linux-gtk-x86_64.tar.gz 2.tar包存在~/Download/下,解压 ...
- LVM逻辑磁盘管理
一.简介 LVM是逻辑盘卷管理(Logical Volume Manager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵 ...
- range开始节点和结束节点
sc = range.startContainer, so = range.startOffsest ec = range.endContainer, eo=range.endOffset sta ...
- HashTable的使用,扑克牌发牌游戏
l 场景 主要实现以下功能: 1. 首先给扑克牌中每张牌设定一个编号,下面算法实现的编号规则如下: 红桃按照从小到大依次为:1-13 方块按照从小到大依次为:14-26 黑桃按 ...
- Linux软件包管理 RMP包
RPM 包的安装虽然很方便和快捷,但是依赖性实在是很麻烦,尤其是库文件依赖,还要去 rpmfind 网站査找库文件到底属于哪个 RPM 包,从而导致 RPM 包的安装非常烦琐.那么,有没有可以自动解决 ...
- Python Streaming实战2: Join的实现与数据过滤
Hadoop Join 与 Not In的实现 (一)源数据与要实现的查询 1. 要实现的查询 select a.sid ,a.name ,b.course ,b.score from Studen ...
- for update排他锁详解
使用场景: 高并发并且对于数据的准确性很有要求. 落实到mysql就是在事务中使用,只有使用InnoDB时才用,在begin于commit之间使用(只有此引擎支持事务). 本质: 给表或行上个锁以便接 ...
- CentOS 安装 Zabbix
一.安装 centos7 网易下载 http://mirrors.163.com/centos/7.2.1511/isos/x86_64/CentOS-7-x86_64-DVD-1511.torren ...
- 【bzoj1345】[Baltic2007]序列问题Sequence
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1345 因为合并的花费是较大数的权值,所以每个数肯定是和附近的小数合并完后才与大数合并, ...
- [Codechef November Challenge 2012] Arithmetic Progressions
题意:给定一个序列,求多少个三元组满足ai+ak=2*aj(i<j<k). 题解:原来叉姐的讲义上有啊..完全忘掉了.. 首先这个式子很明显是一个卷积.我们有了FFT的思路.但是肯定不能每 ...