496e78b522982e3b82c146f43eecbb3fa8b3ffb7.svn-base
3.55 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* Copyright © 2015-2018 ODM All rights reserved.
*/
package com.thinkgem.jeesite.common.security.shiro.cache;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.UnavailableSecurityManagerException;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.Sets;
import com.thinkgem.jeesite.common.web.Servlets;
/**
* 自定义授权缓存管理类
* @author ThinkGem
* @version 2014-7-21
*/
public class SessionCacheManager implements CacheManager {
@Override
public <K, V> Cache<K, V> getCache(String name) throws CacheException {
return new SessionCache<K, V>(name);
}
/**
* SESSION缓存管理类
*/
public class SessionCache<K, V> implements Cache<K, V> {
private Logger logger = LoggerFactory.getLogger(getClass());
private String cacheKeyName = null;
public SessionCache(String cacheKeyName) {
this.cacheKeyName = cacheKeyName;
}
public Session getSession(){
Session session = null;
try{
Subject subject = SecurityUtils.getSubject();
session = subject.getSession(false);
if (session == null){
session = subject.getSession();
}
}catch (InvalidSessionException e){
logger.error("Invalid session error", e);
}catch (UnavailableSecurityManagerException e2){
logger.error("Unavailable SecurityManager error", e2);
}
return session;
}
@SuppressWarnings("unchecked")
@Override
public V get(K key) throws CacheException {
if (key == null){
return null;
}
V v = null;
HttpServletRequest request = Servlets.getRequest();
if (request != null){
v = (V)request.getAttribute(cacheKeyName);
if (v != null){
return v;
}
}
V value = null;
value = (V)getSession().getAttribute(cacheKeyName);
logger.debug("get {} {} {}", cacheKeyName, key, request != null ? request.getRequestURI() : "");
if (request != null && value != null){
request.setAttribute(cacheKeyName, value);
}
return value;
}
@Override
public V put(K key, V value) throws CacheException {
if (key == null){
return null;
}
getSession().setAttribute(cacheKeyName, value);
if (logger.isDebugEnabled()){
HttpServletRequest request = Servlets.getRequest();
logger.debug("put {} {} {}", cacheKeyName, key, request != null ? request.getRequestURI() : "");
}
return value;
}
@SuppressWarnings("unchecked")
@Override
public V remove(K key) throws CacheException {
V value = null;
value = (V)getSession().removeAttribute(cacheKeyName);
logger.debug("remove {} {}", cacheKeyName, key);
return value;
}
@Override
public void clear() throws CacheException {
getSession().removeAttribute(cacheKeyName);
logger.debug("clear {}", cacheKeyName);
}
@Override
public int size() {
logger.debug("invoke session size abstract size method not supported.");
return 0;
}
@Override
public Set<K> keys() {
logger.debug("invoke session keys abstract size method not supported.");
return Sets.newHashSet();
}
@Override
public Collection<V> values() {
logger.debug("invoke session values abstract size method not supported.");
return Collections.emptyList();
}
}
}