Skip to content

Instantly share code, notes, and snippets.

@jamestrandung
Last active December 15, 2015 15:39
Show Gist options
  • Save jamestrandung/5283716 to your computer and use it in GitHub Desktop.
Save jamestrandung/5283716 to your computer and use it in GitHub Desktop.
Redirect after login
String requestURI = request.getRequestURI();
String queryString = request.getQueryString();
String encodedURL = URLEncoder.encode(requestURI + "?" + queryString, "UTF-8");
response.sendRedirect(request.getContextPath() + "/login.xhtml?originalURL=" + encodedURL);
@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());
}
}
}
@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";
}
}
<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>
@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;
}
}
}
@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