Java安全会话

6 java security session

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

Bri*_*ian 9

在会话无效时,您仍然在服务器上.

//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);
Run Code Online (Sandbox Code Playgroud)


Hen*_*rik 2

获取现有的;使其无效;创建一个新的...

1)通过HttpServletRequest.getSession()获取当前Session;
2)清除Session:HttpSession.invalidate();
3) 创建一个新的:HttpServletRequest.getSession(true)