Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TokenHelper |
|
| 0.0;0 |
1 | /* | |
2 | * Copyright 2004-2008 the Seasar Foundation and the Others. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | |
13 | * either express or implied. See the License for the specific language | |
14 | * governing permissions and limitations under the License. | |
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 | * 2重サブミット防止処理のヘルパークラス | |
33 | * | |
34 | * @see TokenTag | |
35 | * @see TokenValidator | |
36 | * @author agata | |
37 | * @since 1.0.0 | |
38 | */ | |
39 | 0 | public class TokenHelper { |
40 | ||
41 | /** | |
42 | * トークン用Mapに保持するトークンの個数(1セッションあたり何個のトークンを保持するか?) | |
43 | */ | |
44 | 1 | public static int TOKEN_HISTORY_SIZE = 16; |
45 | ||
46 | /** | |
47 | * デフォルトのトークン用パラメータ名 | |
48 | */ | |
49 | public static final String DEFAULT_TOKEN_NAME = "cubby.token"; | |
50 | ||
51 | /** | |
52 | * トークン生成用のランダムクラス | |
53 | */ | |
54 | 1 | private static final Random RANDOM = new Random(); |
55 | ||
56 | /** | |
57 | * ユニークなトークンを生成します。 | |
58 | * | |
59 | * @return ユニークなトークン | |
60 | */ | |
61 | public static String generateGUID() { | |
62 | 50004 | return new BigInteger(165, RANDOM).toString(36).toUpperCase(); |
63 | } | |
64 | ||
65 | /** | |
66 | * トークン用のマップをセッションから取得します。 | |
67 | * <p> | |
68 | * セッション中にトークン用のマップが存在しない場合、新規に生成します。 トークン用のマップは{@link LruHashMap}を使い、トークンの保持上限付きのMapになります。 | |
69 | * </p> | |
70 | * | |
71 | * @param session | |
72 | * セッション | |
73 | * @return トークン用のマップ | |
74 | */ | |
75 | @SuppressWarnings("unchecked") | |
76 | public static Map<String, String> getTokenMap(HttpSession session) { | |
77 | 16 | Map<String, String> tokenMap = (Map<String, String>) session |
78 | .getAttribute(CubbyConstants.ATTR_TOKEN); | |
79 | 16 | if (tokenMap == null) { |
80 | 5 | tokenMap = new LruHashMap(TOKEN_HISTORY_SIZE); |
81 | 5 | session.setAttribute(ATTR_TOKEN, tokenMap); |
82 | } | |
83 | 16 | return tokenMap; |
84 | } | |
85 | ||
86 | /** | |
87 | * トークンをセッション中のトークン用のMapにセットします。 | |
88 | * | |
89 | * @param session | |
90 | * セッション | |
91 | * @param token | |
92 | * トークン文字列 | |
93 | */ | |
94 | public static void setToken(HttpSession session, String token) { | |
95 | 6 | Map<String, String> tokenMap = getTokenMap(session); |
96 | 6 | synchronized (tokenMap) { |
97 | 6 | tokenMap.put(token, null); |
98 | 6 | } |
99 | 6 | } |
100 | ||
101 | /** | |
102 | * パラメータ中のトークン文字列とセッション中のトークン文字列を検証します。 | |
103 | * <p> | |
104 | * セッション中に格納されたトークン用のMapのキーに、 指定されたトークン文字列が含まれるかどうかを判定し、Mapから取り除きます。 | |
105 | * </p> | |
106 | * | |
107 | * @param session | |
108 | * セッション | |
109 | * @param token | |
110 | * トークン文字列 | |
111 | * @return 指定されたトークン文字列がセッション中に存在したら<code>true</code>、それ以外は<code>false</code> | |
112 | */ | |
113 | public static boolean validateToken(HttpSession session, String token) { | |
114 | 10 | Map<String, String> tokenMap = getTokenMap(session); |
115 | 10 | synchronized (tokenMap) { |
116 | 10 | boolean success = tokenMap.containsKey(token); |
117 | 10 | tokenMap.remove(token); |
118 | 10 | return success; |
119 | 0 | } |
120 | } | |
121 | } |