1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.cubby.util;
17
18 import static org.seasar.cubby.CubbyConstants.ATTR_TOKEN;
19
20 import java.math.BigInteger;
21 import java.util.Map;
22 import java.util.Random;
23
24 import javax.servlet.http.HttpSession;
25
26 import org.seasar.cubby.CubbyConstants;
27 import org.seasar.cubby.tags.TokenTag;
28 import org.seasar.cubby.validator.validators.TokenValidator;
29 import org.seasar.framework.util.LruHashMap;
30
31
32
33
34
35
36
37 public class TokenHelper {
38
39
40
41
42 public static int TOKEN_HISTORY_SIZE = 16;
43
44
45
46
47 public static final String DEFAULT_TOKEN_NAME = "cubby.token";
48
49
50
51
52 private static final Random RANDOM = new Random();
53
54
55
56
57
58 public static String generateGUID() {
59 return new BigInteger(165, RANDOM).toString(36).toUpperCase();
60 }
61
62
63
64
65
66
67
68
69
70
71 @SuppressWarnings("unchecked")
72 public
73 static Map<String, String> getTokenMap(HttpSession session) {
74 Map<String, String> tokenMap = (Map<String, String>) session.getAttribute(CubbyConstants.ATTR_TOKEN);
75 if (tokenMap == null) {
76 tokenMap = new LruHashMap(TOKEN_HISTORY_SIZE);
77 session.setAttribute(ATTR_TOKEN, tokenMap);
78 }
79 return tokenMap;
80 }
81
82
83
84
85
86
87 public static void setToken(HttpSession session, String token) {
88 Map<String, String> tokenMap = getTokenMap(session);
89 synchronized (tokenMap) {
90 tokenMap.put(token, null);
91 }
92 }
93
94
95
96
97
98
99
100
101
102
103
104 public static boolean validateToken(HttpSession session,
105 String token) {
106 Map<String, String> tokenMap = getTokenMap(session);
107 synchronized (tokenMap) {
108 boolean success = tokenMap.containsKey(token);
109 tokenMap.remove(token);
110 return success;
111 }
112 }
113 }