简体   繁体   English

Java 中的会话劫持预防 (Struts 2.0) | 遇到错误

[英]Session Hijacking Prevention in Java (Struts 2.0) | Error Encountered

I'm developing an application in Java which seems to have a session hijacking vulnerability.我正在用 Java 开发一个应用程序,它似乎有一个会话劫持漏洞。

In order to prevent this, the recommendation is to change the JSESSIONID for a user after log in为了防止这种情况,建议在登录后更改用户的JSESSIONID

My application is based on Struts 2.0 and Tomcat 7 and I have implemented a code to change the JSESSIONID after the user logs in.我的应用程序基于 Struts 2.0 和 Tomcat 7,我已经实现了一个代码来在用户登录后更改JSESSIONID

However I am facing the following problem while running the code.但是,我在运行代码时面临以下问题。

java.lang.IllegalStateException: setAttribute: Session already invalidated
at org.apache.catalina.session.StandardSession.setAttribute(StandardSession.java:1289)
at org.apache.catalina.session.StandardSession.setAttribute(StandardSession.java:1254)
at org.apache.catalina.session.StandardSessionFacade.setAttribute          (StandardSessionFacade.java:130)
at org.apache.struts2.dispatcher.SessionMap.put(SessionMap.java:181)

Here is the code that I wrote :这是我写的代码:

HttpSession httpSession = ServletActionContext.getRequest().getSession();
HashMap<String, Object> attributes = new HashMap<String, Object>(); 
Enumeration<String> enames = httpSession.getAttributeNames();
while ( enames.hasMoreElements() )
{
String name = enames.nextElement();   
if ( !name.equals( "JSESSIONID" ) )
{ 
attributes.put( name, httpSession .getAttribute( name ) );
}      
}   
httpSession.invalidate();       
httpSession = request.getSession(true);                     
for ( Map.Entry<String, Object> et : attributes.entrySet() )
{
userInfoMap.put( et.getKey(), et.getValue() );
}   
getSession().put("userid",userId);//Setting value to session

Usually when you invalidate the session you should redirect to some action, so the new session map will injected to it if the action implement SessionAware .通常,当您invalidate会话invalidate ,您应该重定向到某个操作,因此如果该操作实现SessionAware ,则新的会话映射将注入它。

But in the code you posted you are trying to reuse the session map while it contains an old session.但是在您发布的代码中,您试图在包含旧会话时重用会话映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM