CSRF攻击防范

我们当时是根据sessionId生成的一个token值,放入表单中。
生成方法如下:
----------------------------------------
	private static SecureRandom secureRandom=null;
	public static String createToken() {
		if(secureRandom==null){
			String entoropy="LogonSessionEntoropy" + System.currentTimeMillis();
			try {
				secureRandom = SecureRandom.getInstance("SHA1PRNG");
			} catch (NoSuchAlgorithmException e) {
				throw new RuntimeException(e);
			}
			secureRandom.setSeed(entoropy.getBytes());
		}
		byte bytes[]=new byte[16];
		secureRandom.nextBytes(bytes);
		byte[] base64Bytes = Base64.encode(bytes);
		return new String(base64Bytes);
	}

  

 将生成的随机值放入session中, 然后每次来请求的,都判断发送的这个随机值是否与session中保存的一致就行了。
posted on 2013-02-19 15:58  在大地画满窗子  阅读(587)  评论(0编辑  收藏  举报