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 方 ...
随机推荐
- 异常:Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005.
异常:Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} ...
- GP DBA基本操作
1.查看队列情况 SELECT * FROM gp_toolkit.gp_resqueue_status; 如果出现了资源占用大于1.1 e+11SQL,则可能存在不合理执行计划的SQL, 基本跑不出 ...
- HTML5模拟衣服撕扯动画
在线演示 本地下载
- Class文件结构(更新中)
Class文件是一组以8位字节为单位的二进制流,当遇见需要占用8位字节以上空间的数据项时,则会按照高位在前的方式分割成若干个8位字节进行存储. 格式,采用伪结构,只有两种数据结构:无符号数和表. 无符 ...
- DP问题分类总结
http://m.blog.csdn.net/y990041769/article/details/24194605 此博客总结了动态规划相关问题,学习一下!
- jsonp 实现跨域例子
直接上代码: js: <html> <head> <title>JSONP</title> </head> <script src = ...
- Exception in thread "main" java.util.concurrent.ExecutionException: org.apache.kafka.common.errors.TimeoutException: Expiring 1 record(s) for topic_test_1219-2: 30010 ms has passed since batch creatio
代码如下 public static void producer1() throws ExecutionException, InterruptedException { Properties pro ...
- HDU 2480 Steal the Treasure (并查集+贪心)
题意:给你n个点,m条边,包括有向边与无向边,每条边都有一个权值.在每个点上都有一个人,他可以走与这个点直接相连的所有边中任意一条边一次,并且得到这个权值,就不能走了,注意这条路也只能被一个人走.问最 ...
- [转]Android:改变Activity切换方式
overridePendingTransition(enterAnim, exitAnim); Intent intent =new Intent(this,item2.class); startAc ...
- table-layout 属性
最近被测试提了一个bug,表单的某个字段有1300的字数限制,测试填了1300字,提交后,表格上的呈现丑爆了,那个字段的所在的列撑满了整个表格,其他列被压缩的很小. 后来知道了table-layout ...