-
Notifications
You must be signed in to change notification settings - Fork 477
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7111 from GlobalDataverseCommunityConsortium/IQSS…
…/3254 adding support for changing sessionId at login
- Loading branch information
Showing
3 changed files
with
38 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/edu/harvard/iq/dataverse/util/SessionUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package edu.harvard.iq.dataverse.util; | ||
|
||
import java.util.Enumeration; | ||
import java.util.HashMap; | ||
import java.util.Map.Entry; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpSession; | ||
|
||
public class SessionUtil { | ||
|
||
/** | ||
* Changes the session id (jsessionId) - for use when the session's authority increases (i.e. at login) | ||
* Servlet 3.1 Note: This method is needed while using Servlets 2.0. 3.1 has a HttpServletRequest.chageSessionId(); method that can be used instead. | ||
* | ||
* @param h the current HttpServletRequest | ||
* e.g. for pages you can get this from (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(); | ||
*/ | ||
public static void changeSessionId(HttpServletRequest h) { | ||
HttpSession session = h.getSession(false); | ||
HashMap<String, Object> sessionAttributes = new HashMap<String,Object>(); | ||
for(Enumeration<String> e = session.getAttributeNames();e.hasMoreElements();) { | ||
String name = e.nextElement(); | ||
sessionAttributes.put(name, session.getAttribute(name)); | ||
} | ||
h.getSession().invalidate(); | ||
session = h.getSession(true); | ||
for(Entry<String, Object> entry: sessionAttributes.entrySet()) { | ||
session.setAttribute(entry.getKey(), entry.getValue()); | ||
} | ||
} | ||
} |