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 方 ...
随机推荐
- rails 下载 send_file
def download send_file File.join(Rails.root, "public", @doc.link), :filename => @title+ ...
- javascript;Jquery;获取JSON对象,无刷新评论实例。
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> < ...
- vi的搜索和替换
搜索中进行替换 /which #搜索which cwthat #替换成that n #重复搜索 . #重复替换 一种类型的替换命令 g/pattern/s/old/new/g 第一个 g 表示是有选择 ...
- 【leetcode刷题笔记】Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 前端自动化构建工具-gulp
gulp 和grunt这两个是我知道的自动构建工具,但是说实话都没在项目中用过,不太清楚自动化构建是什么意思, 1.grunt和gulp有什么相同点和不同点? (1).易于使用:采用代码优于配置策略, ...
- 2018.7.12训练赛 -G
第二道水题 前边说了很多话,但就最后两段有用. 就是给你一个序列,然后你判断一下这个序列是不是递增的,是就输出yes,否则输出no. 所以以后不管题目看起来多长.多复杂,都要读一遍. 代码就不贴了.
- linux文件系统实现原理简述【转】
本文转载自:https://blog.csdn.net/eleven_xiy/article/details/71249365 [摘要] [背景] [正文] [总结] 注意:请使用谷歌浏览器阅读( ...
- Python 循环的综合应用
# 循环综合应用1. # str = "hello,world" 把字符串给反转显示 str = "hello,world" temp = "&quo ...
- StringTemplateLoader的用法
作为一个模板框架,freemarker的功能还是很强大的.在模板处理方面,freemarker有多种形式,最常见的方式是将模板文件放在一个统一的文件夹下面,如下形式:Configuration cfg ...
- Eclipse4.2安装样式插件
1.插件地址 http://eclipse-color-theme.github.com/update 点击Eclipse菜单 Help>>Install New Software... ...