簡體   English   中英

Java 中的會話劫持預防 (Struts 2.0) | 遇到錯誤

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

我正在用 Java 開發一個應用程序,它似乎有一個會話劫持漏洞。

為了防止這種情況,建議在登錄后更改用戶的JSESSIONID

我的應用程序基於 Struts 2.0 和 Tomcat 7,我已經實現了一個代碼來在用戶登錄后更改JSESSIONID

但是,我在運行代碼時面臨以下問題。

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)

這是我寫的代碼:

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

通常,當您invalidate會話invalidate ,您應該重定向到某個操作,因此如果該操作實現SessionAware ,則新的會話映射將注入它。

但是在您發布的代碼中,您試圖在包含舊會話時重用會話映射。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM