我的应用程序使用java servlets、jsp和tomcat6。我喜欢实现会话id的更改,并希望在登录后将旧的会话属性复制到新的会话属性中。我们开始在这里面使用了一点弹簧。这是将此功能添加到已有10年历史的应用程序的最佳方式。
发布于 2011-05-18 19:24:20
如果您使用Spring Security,默认情况下,框架应该在登录后更改会话id。
@查看Spring Security FAQ
当我通过Spring Security进行身份验证时,会话Id为什么会改变?
使用默认配置,Spring Security会在用户进行身份验证时使现有会话无效,并创建一个新会话,将会话数据传输给它。其目的是更改会话标识符,以防止“会话固定”攻击。您可以在在线和参考手册中找到有关此内容的更多信息
如果你不使用Spring (安全),你必须自己去做。有点像这样:
public class Login extends HttpServlet {
...
HttpSession session = request.getSession();
Map<String,Object> values = session.GetAll(); //This line is psydo code
//Use getValueNames() and a loop with getValue(String name);
// Kill the current session
session.invalidate();
HttpSession newSession = request.getSession(true);
newSession.putAllValues(values); //This line is psydo code
... 发布于 2016-12-31 14:30:01
session=request.getSession(true);
Enumeration keys = session.getAttributeNames();
HashMap<String,Object> hm=new HashMap<String,Object>();
while (keys.hasMoreElements())
{
String key = (String)keys.nextElement();
hm.put(key,session.getValue(key));
session.removeAttribute(key);
}
session.invalidate();
session=request.getSession(true);
for(Map.Entry m:hm.entrySet())
{
session.setAttribute((String)m.getKey(),m.getValue());
hm.remove(m);
} 发布于 2012-08-13 14:17:41
这可能会有帮助
Cookie cookie = new Cookie("JSESSIONID", null);
cookie.setPath("/");
cookie.setMaxAge(0);
response.addProperty(cookie);https://stackoverflow.com/questions/6042846
复制相似问题