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 方 ...
随机推荐
- Java进阶学习:将文件上传到七牛云中
Java进阶学习:将文件上传到七牛云中 通过本文,我们将讲述如何利用七牛云官方SDK,将我们的本地文件传输到其存储空间中去. JavaSDK:https://developer.qiniu.com/k ...
- IBM的SOA方法论之一——五个切入点和八个场景
一.什么是SOA: 面向服务的体系结构(Service-Oriented Architecture,SOA)是一种 IT 体系结构风格,支持将您的业务转换为一组相互链接的服务或可重复业务任务,可在需要 ...
- 026_默认的MapReduce Driver(最小驱动问题)
1. 最小配置的MapReduce Driver 读取输入文件中的内容,输出到指定目录的输出文件中,此时文件中的内容为: Key---输入文件每行内容的起始位置. Value---输入文件每行的原始内 ...
- INSPIRED启示录 读书笔记 - 第21章 产品验证
证明产品的价值.可用性.可行性 产品验证是指在正式开发.部署产品前,验证产品说明文档描述的产品是否符合预期要求 产品经理向产品团队提供最终的产品说明文档前,需要进行三项重要验证 1.可行性测试:明确在 ...
- Pandas的 loc iloc ix 区别
先看代码: In [46]: import pandas as pd In [47]: data = [[1,2,3],[4,5,6]] In [48]: index = [0,1] In [49]: ...
- 通用Mapper(Mybatis)
1.Mapper的简单介绍 2.Mapper的作用 通用Mapper可以通过Mybatis的拦截器原理,动态的帮我们实现单表的增删改查功能,大大降低了我们的开发成本,减少了我们的工作量. 3.Mapp ...
- [转载]spring security 的 logout 功能
原文地址:security 的 logout 功能">spring security 的 logout 功能作者:sumnny 转载自:http://lengyun3566.iteye ...
- java基础10(IO流)-字节流
IO流 输入与输出[参照物是程序] 如果从键盘.文件.网络甚至是另一个进程(程序或系统)将数据读入到程序或系统中,称为输入 如果是将程序或系统中的数据写到屏幕.硬件上的文件.网络上的另一端或者是一个进 ...
- centos下安装python2.7.9和pip以及数据科学常用的包
以前一直用ubantu下的python,ubantu比较卡.自己倾向于使用centos,但默认的python版本太低,所以重新装了一个python和ipython centos6.5安装python2 ...
- 导入Jquery.min.js时 JQuery 上打红X了
问题解决:右击jquery.min.js——>MyEclipse——>点击Exclude From Validation——>点击Run Validation 即可