View Javadoc

1   /*
2    * Copyright 2004-2009 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.internal.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  /**
27   * 2重サブミット防止処理のヘルパークラス
28   * 
29   * @see org.seasar.cubby.tags.TokenTag
30   * @see org.seasar.cubby.validator.validators.TokenValidator
31   * @author agata
32   */
33  public class TokenHelper {
34  
35  	/**
36  	 * トークン用Mapに保持するトークンの個数(1セッションあたり何個のトークンを保持するか?)
37  	 */
38  	private static final int TOKEN_HISTORY_SIZE = 16;
39  
40  	/**
41  	 * デフォルトのトークン用パラメータ名
42  	 */
43  	public static final String DEFAULT_TOKEN_NAME = "cubby.token";
44  
45  	/**
46  	 * トークン生成用のランダムクラス
47  	 */
48  	private static final Random RANDOM = new Random();
49  
50  	/**
51  	 * ユニークなトークンを生成します。
52  	 * 
53  	 * @return ユニークなトークン
54  	 */
55  	public static String generateGUID() {
56  		return new BigInteger(165, RANDOM).toString(36).toUpperCase();
57  	}
58  
59  	/**
60  	 * トークン用の <code>Map</code> をセッションから取得します。
61  	 * <p>
62  	 * セッション中にトークン用の <code>Map</code> が存在しない場合、新規に生成します。 トークン用のマップは
63  	 * {@link LruHashMap} を使い、トークンの保持上限付きの <code>Map</code> になります。
64  	 * </p>
65  	 * 
66  	 * @param session
67  	 *            セッション
68  	 * @return トークン用のマップ
69  	 */
70  	@SuppressWarnings("unchecked")
71  	public static Map<String, String> getTokenMap(final HttpSession session) {
72  		Map<String, String> tokenMap = (Map<String, String>) session
73  				.getAttribute(ATTR_TOKEN);
74  		if (tokenMap == null) {
75  			tokenMap = new LruHashMap<String, String>(TOKEN_HISTORY_SIZE);
76  			session.setAttribute(ATTR_TOKEN, tokenMap);
77  		}
78  		return tokenMap;
79  	}
80  
81  	/**
82  	 * トークンをセッション中のトークン用の <code>Map</code> にセットします。
83  	 * 
84  	 * @param session
85  	 *            セッション
86  	 * @param token
87  	 *            トークン文字列
88  	 */
89  	public static void setToken(final HttpSession session, final String token) {
90  		final Map<String, String> tokenMap = getTokenMap(session);
91  		synchronized (tokenMap) {
92  			tokenMap.put(token, null);
93  		}
94  	}
95  
96  	/**
97  	 * パラメータ中のトークン文字列とセッション中のトークン文字列を検証します。
98  	 * <p>
99  	 * セッション中に格納されたトークン用の <code>Map</code> のキーに指定されたトークン文字列が含まれるかどうかを判定し、
100 	 * <code>Map</code> から取り除きます。
101 	 * </p>
102 	 * 
103 	 * @param session
104 	 *            セッション
105 	 * @param token
106 	 *            トークン文字列
107 	 * @return 指定されたトークン文字列がセッション中に存在したら<code>true</code>、それ以外は
108 	 *         <code>false</code>
109 	 */
110 	public static boolean validateToken(final HttpSession session,
111 			final String token) {
112 		final Map<String, String> tokenMap = getTokenMap(session);
113 		synchronized (tokenMap) {
114 			final boolean success = tokenMap.containsKey(token);
115 			tokenMap.remove(token);
116 			return success;
117 		}
118 	}
119 
120 }