|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.acegisecurity.providers.cas.cache; |
|
17 |
| |
|
18 |
| import org.acegisecurity.providers.cas.CasAuthenticationToken; |
|
19 |
| import org.acegisecurity.providers.cas.StatelessTicketCache; |
|
20 |
| |
|
21 |
| import net.sf.ehcache.Cache; |
|
22 |
| import net.sf.ehcache.CacheException; |
|
23 |
| import net.sf.ehcache.Element; |
|
24 |
| |
|
25 |
| import org.apache.commons.logging.Log; |
|
26 |
| import org.apache.commons.logging.LogFactory; |
|
27 |
| |
|
28 |
| import org.springframework.beans.factory.InitializingBean; |
|
29 |
| |
|
30 |
| import org.springframework.dao.DataRetrievalFailureException; |
|
31 |
| import org.springframework.util.Assert; |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| public class EhCacheBasedTicketCache implements StatelessTicketCache, |
|
42 |
| InitializingBean { |
|
43 |
| |
|
44 |
| |
|
45 |
| private static final Log logger = LogFactory.getLog(EhCacheBasedTicketCache.class); |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| private Cache cache; |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
4
| public CasAuthenticationToken getByTicketId(String serviceTicket) {
|
|
54 |
4
| Element element = null;
|
|
55 |
| |
|
56 |
4
| try {
|
|
57 |
4
| element = cache.get(serviceTicket);
|
|
58 |
| } catch (CacheException cacheException) { |
|
59 |
0
| throw new DataRetrievalFailureException("Cache failure: "
|
|
60 |
| + cacheException.getMessage()); |
|
61 |
| } |
|
62 |
| |
|
63 |
4
| if (logger.isDebugEnabled()) {
|
|
64 |
0
| logger.debug("Cache hit: " + (element != null)
|
|
65 |
| + "; service ticket: " + serviceTicket); |
|
66 |
| } |
|
67 |
| |
|
68 |
4
| if (element == null) {
|
|
69 |
3
| return null;
|
|
70 |
| } else { |
|
71 |
1
| return (CasAuthenticationToken) element.getValue();
|
|
72 |
| } |
|
73 |
| } |
|
74 |
| |
|
75 |
2
| public void setCache(Cache cache) {
|
|
76 |
2
| this.cache = cache;
|
|
77 |
| } |
|
78 |
| |
|
79 |
1
| public Cache getCache() {
|
|
80 |
1
| return cache;
|
|
81 |
| } |
|
82 |
| |
|
83 |
2
| public void afterPropertiesSet() throws Exception {
|
|
84 |
2
| Assert.notNull(cache, "cache mandatory");
|
|
85 |
| } |
|
86 |
| |
|
87 |
1
| public void putTicketInCache(CasAuthenticationToken token) {
|
|
88 |
1
| Element element = new Element(token.getCredentials().toString(), token);
|
|
89 |
| |
|
90 |
1
| if (logger.isDebugEnabled()) {
|
|
91 |
0
| logger.debug("Cache put: " + element.getKey());
|
|
92 |
| } |
|
93 |
| |
|
94 |
1
| cache.put(element);
|
|
95 |
| } |
|
96 |
| |
|
97 |
1
| public void removeTicketFromCache(CasAuthenticationToken token) {
|
|
98 |
1
| if (logger.isDebugEnabled()) {
|
|
99 |
0
| logger.debug("Cache remove: " + token.getCredentials().toString());
|
|
100 |
| } |
|
101 |
| |
|
102 |
1
| this.removeTicketFromCache(token.getCredentials().toString());
|
|
103 |
| } |
|
104 |
| |
|
105 |
1
| public void removeTicketFromCache(String serviceTicket) {
|
|
106 |
1
| cache.remove(serviceTicket);
|
|
107 |
| } |
|
108 |
| } |