Last active
December 15, 2015 15:39
-
-
Save jamestrandung/5283716 to your computer and use it in GitHub Desktop.
Redirect after login
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
String requestURI = request.getRequestURI(); | |
String queryString = request.getQueryString(); | |
String encodedURL = URLEncoder.encode(requestURI + "?" + queryString, "UTF-8"); | |
response.sendRedirect(request.getContextPath() + "/login.xhtml?originalURL=" + encodedURL); |
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
@Named(value = "loginBean") | |
@ViewScoped | |
public class LoginBean implements Serializable { | |
@ManagedProperty(value = "#{userSession}") | |
private UserSessionBean userSession; | |
private String originalURL; | |
@PostConstruct | |
public void init() { | |
this.originalURL = userSession.getOriginalURL(); | |
} | |
public void login() { | |
try { | |
// Perform login logic | |
... | |
// Redirect the user back to where they have been after successful login | |
FacesContext.getCurrentInstance().getExternalContext().redirect(originalURL); | |
} catch (IOException ex) { | |
System.out.println("UserSessionBean - Exception: " + ex.toString()); | |
} | |
} | |
} |
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
@PostConstruct | |
public void init() { | |
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); | |
HttpServletRequest request = (HttpServletRequest) externalContext.getRequest(); | |
this.originalURL = userSession.getOriginalURL(); | |
if (originalURL == null) { | |
// Redirect to home page in case the user didn't surf any pages before logging in | |
this.originalURL = request.getContextPath() + "home.xhtml"; | |
} | |
} |
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
<f:view> | |
<ui:param name="originalURL" value="#{request.requestURI}?#{request.queryString}" /> | |
<f:metadata> | |
<f:event rendered="#{not userSession.loggedIn}" type="preRenderView" listener="#{userSession.recordOriginalURL(originalURL)}" /> | |
</f:metadata> | |
</f:view> |
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
@PostConstruct | |
public void init() { | |
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); | |
HttpServletRequest request = (HttpServletRequest) externalContext.getRequest(); | |
String forwardedRequestURI = (String) externalContext.getRequestMap().get(RequestDispatcher.FORWARD_REQUEST_URI); | |
if ((this.originalURL = request.getParameter("originalURL")) != null) { | |
// If the user was redirected, retrieve the originalURL from the request's "originalURL" parameter | |
return; | |
} else if (forwardedRequestURI == null) { | |
// If the user logged in directly from the top bar, simply redirect to the originalURL recorded by UserSessionBean | |
this.originalURL = userSession.getOriginalURL(); | |
if (originalURL == null) { | |
// Redirect to home page in case the user didn't surf any pages before logging in | |
this.originalURL = request.getContextPath() + "home.xhtml"; | |
} | |
} else { | |
// If the user was forwarded to the login page, re-build the orignal requestURL | |
this.originalURL = forwardedRequestURI; | |
String originalQuery = (String) externalContext.getRequestMap().get(RequestDispatcher.FORWARD_QUERY_STRING); | |
if (originalQuery != null) { | |
this.originalURL += "?" + originalQuery; | |
} | |
} | |
} |
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
@ManagedBean(value = "userSession") | |
@SessionScoped | |
public class UserSessionBean implements Serializable { | |
private boolean loggedIn; | |
private String originalURL; | |
public void recordOriginalURL(String originalURL) { | |
this.originalURL = originalURL; | |
} | |
// Getters and Setters | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment