Java安全会话

时间:2009-07-22 06:08:19

标签: java security session

每当您进行身份验证时,您的应用程序都应更改其使用的会话标识符。这有助于防止某人设置会话,复制会话标识符,然后欺骗用户使用会话。由于攻击者已经知道会话标识符,因此他们可以在用户登录后使用它来访问会话,从而为其提供完全访问权限。这种攻击被称为“会话固定”等。一旦用户登录系统,如何更改会话ID?

4 个答案:

答案 0 :(得分:8)

当您使会话无效时,您仍然在服务器上。

//get stuff out of session you want before invalidating it.
currentSession = request.getSession(true);
UserProfile userProfile = (UserProfile) currentSession.getAttribute("userProfile");

//now invalidate it
currentSession.invalidate();

//get new session and stuff the data back in
HttpSession newSession = request.getSession(true);
newSession.setAttribute("userProfile", userProfile);

答案 1 :(得分:2)

获得现有的;使它无效;创造一个新的...

1)使用HttpServletRequest.getSession()获取当前会话;
2)清除会话:HttpSession.invalidate();
3)创建一个新的:HttpServletRequest.getSession(true);

答案 2 :(得分:2)

通常说话(因为这根本不是Java问题,这是一般的网络问题)会话固定在会话ID易于发现或猜测时出现。主要攻击方法是会话ID位于页面的URL中,例如http://example.com/index?sessionId=123。攻击者可以设置捕获会话,然后将链接嵌入其页面,诱使用户访问它并成为其会话的一部分。然后,当用户进行身份验证时,会话进行身份验证。缓解此问题的方法是不使用基于URL的会话ID,而是使用cookie

某些Web应用程序将使用基于cookie的会话,但是会从初始URL设置它,例如访问http://example.com/index?sessionId=123会在URL中看到会话ID,然后从中创建会话cookie,在其中设置id会话cookie到123.缓解这种情况是在服务器上生成随机会话ID,而不使用任何用户输入作为生成器的种子。

还有基于浏览器的漏洞,其中编码不良的浏览器将接受不是原始域的域的cookie创建,但是你可以做的不多。并且跨站点脚本攻击可以将脚本命令发送到受攻击的站点以设置会话cookie,可以通过将会话cookie设置为HTTP_ONLY来缓解(尽管Safari不遵守此标志)

对于Java,一般建议是

session.invalidate();
session=request.getSession(true); 

然而,在JBoss的某一点上,这没有用 - 所以你需要在你选择的框架内按预期检查它。

答案 3 :(得分:0)

使当前会话无效并获得新会话:

//invalidate the current session
request.getSession().invalidate();
/*
get another session and get the ID (getSession()) will create a session if one does not exist
*/
request.getSession().getId();