QiYuAdmin-SpringBoot之Shiro在线会话持久化
2017-04-06 10:19
309 查看
简介
要点如下:SpringBoot集成shiro的配置
shiro结合ehcache
持久化SessionDAO
前端session列表展现
成果如下
具体实现
Ehcache
@Bean public EhCacheManager cacheManager() { EhCacheManager cacheManager = new EhCacheManager(); cacheManager.setCacheManagerConfigFile("classpath:config/ehcache.xml"); return cacheManager; }1
2
3
4
5
6
1
2
3
4
5
6
<ehcache> <diskStore path="java.io.tmpdir" /> <defaultCache maxElementsInMemory="10000" eternal="false" timeToLiveSeconds="0" timeToIdleSeconds="0" overflowToDisk="false" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" /> <!-- sessioncache --> <cache name="shiro-activeSessionCache" maxEntriesLocalHeap="10000" overflowToDisk="false" eternal="false" diskPersistent="false" timeToLiveSeconds="0" timeToIdleSeconds="0" statistics="true"/> </ehcache>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
以上的ehcache的配置里面请注意两个参数。timeToLiveSeconds=x:缓存自创建日期起至失效时的间隔时间x。timeToIdleSeconds=y:缓存创建以后,最后一次访问缓存的日期至失效之时的时间间隔y。例子:
timeToIdleSeconds=120;
timeToLiveSeconds=180;
上面的表示此缓存最多可以存活3分钟,如果期间超过2分钟未访问 那么此缓存失效!
所以如果你设置以上参数那么你的session将会在2分钟没有访问的时候失效,刷新将会到登录页面。
自定义sessionDAO代码
public class QiYuSessionDAO extends CachingSessionDAO { @Resource private SessionService sessionService; @Override protected void doUpdate(Session session) { if(session instanceof ValidatingSession && !((ValidatingSession)session).isValid()) { return; //如果会话过期/停止 没必要再更新了 } boolean isGuest = session.getAttribute("loginName") == null;//是否是游客 if(isGuest){//是游客,还没有登录,那就不更新 return; } QiYuSession sessionModel = new QiYuSession(); sessionModel.setSessionId(session.getId().toString()); sessionModel.setIp(session.getHost()); sessionModel.setSessionValue(SerializableUtils.serialize(session)); sessionModel.setUpdateDate(new Date()); sessionModel.setId(session.getId().toString()); sessionModel.setLoginName(session.getAttribute("loginName").toString()); sessionModel.setLastAccessTime(session.getLastAccessTime()); try { sessionService.updateByPrimaryKeySelective(sessionModel); } catch (Exception e) { e.printStackTrace(); } } @Override protected void doDelete(Session session) { try { sessionService.deleteByPrimaryKey(session.getId()); } catch (Exception e) { e.printStackTrace(); } } @Override protected Serializable doCreate(Session session) {//只要第一次访问了项目就有了一次会话,就会产生一session,但此时为无效session,游客身份 Serializable sessionId = generateSessionId(session); assignSessionId(session, sessionId); QiYuSession sessionModel = new QiYuSession(); sessionModel.setSessionId(sessionId.toString()); sessionModel.setIp(session.getHost()); sessionModel.setSessionValue(SerializableUtils.serialize(session)); sessionModel.setCreateBy("sessionManager"); sessionModel.setCreateDate(new Date()); sessionModel.setActiveFlag(1); sessionModel.setId(sessionId.toString()); sessionModel.setLoginName("guest");//游客身份,还没有登录 try { sessionService.insertSelective(sessionModel); return session.getId(); } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected Session doReadSession(Serializable sessionId) { QiYuSession session = new QiYuSession(); session.setSessionId(sessionId.toString()); try { List<QiYuSession> sessionList = sessionService.select(session); if(sessionList.size() == 0) return null; return SerializableUtils.deserialize(sessionList.get(0).getSessionValue()); } catch (Exception e) { e.printStackTrace(); } return null; } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
自定义sessionDAO注入
@Bean(value = "sessionDAO") public QiYuSessionDAO sessionDAO(){ QiYuSessionDAO qiYuSessionDAO = new QiYuSessionDAO(); qiYuSessionDAO.setActiveSessionsCacheName("shiro-activeSessionCache"); qiYuSessionDAO.setCacheManager(cacheManager()); return qiYuSessionDAO; }1
2
3
4
5
6
7
1
2
3
4
5
6
7
sessionManager
/** * @see DefaultWebSessionManager * @return */ @Bean(name="sessionManager") public DefaultWebSessionManager defaultWebSessionManager() { DefaultWebSessionManager sessionManager = new DefaultWebSessionManager a251 (); sessionManager.setCacheManager(cacheManager()); sessionManager.setGlobalSessionTimeout(1800000);//超时时间 sessionManager.setSessionValidationSchedulerEnabled(true);//定时清除无效的session sessionManager.setDeleteInvalidSessions(true);//删除无效的session sessionManager.setSessionDAO(sessionDAO()); return sessionManager; }1
2
3
4
5
6
7
8
9
10
11
12
13
14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
前端页面
PS:由于之前封装了表格代码所以用起来非常方便,前端所有代码如下结语
后续更新部门增删改查。转自:山竹之七语: QiYuAdmin-SpringBoot之Shiro在线会话持久化
相关文章推荐
- QiYuAdmin-Quartz定时器动态启停服务(SpringBoot项目实战)
- spring boot + webSocket 实现简单会话与在线人数统计的demo
- spring boot 1.5.4 集成shiro+cas,实现单点登录和权限控制
- Spring Boot 集成Shiro和CAS
- Spring Boot Shiro权限管理--自定义 FormAuthenticationFilter验证码整合
- Spring Boot Shiro 权限管理
- Spring Boot系列(十五) 安全框架Apache Shiro(三)RememberMe
- spring boot 集成shiro的配置
- Spring Boot Shiro 权限信息缓存处理,记住我,thymleaf使用shiro标签
- 关于Spring Boot下Spring Security权限访问设置@PreAuthorize("hasRole('ROLE_ADMIN')")没有用
- Spring-boot-admin功能说明
- Spring Boot 集成Shiro和CAS
- Shiro学习(24)在线会话管理
- (39.4) Spring Boot Shiro权限管理【从零开始学Spring Boot】
- Spring Boot Admin Reference Guide
- Spring Boot Shiro 权限信息缓存处理,记住我,thymleaf使用shiro标签
- Spring Boot Shiro 权限管理
- Spring Boot Admin Reference Guide
- springboot与shiro配置
- Spring Boot系列(十五) 安全框架Apache Shiro(一)基本功能