How to redirect back to the access page to the user's desire after logging in?

Before a user logs in, if the type is xxx.detail.jsf , he will be redirected to the login page to enter the system. This task has already been completed. How can I make it redirect back to xxx.detail.jsf after a user successfully logs in?

I am using Eclipse Indigo, Tomcat 7 and Mojarra 2.0.3.

+6
source share
1 answer

The moment you are redirected to the login page, you need to save the current request URI. You are probably using Filter to do login validation and redirection. In this case, you can use HttpServletRequest#getRequestURI() to get the URI of the current request:

 String requestURI = request.getRequestURI(); 

You can either pass it as a request parameter to the redirect URL, or save it in the session. Passing as a request parameter is most secure:

 response.sendRedirect(request.getContextPath() + "/login.jsf?from=" + URLEncoder.encode(requestURI, "UTF-8")); 

In the bean associated with the login page, you can set it as a managed property or view parameter. Suppose a bean is a viewport that allows you to perform nice ajax actions / validations, etc. In this case, the view parameter is the only neat way:

 <f:metadata> <f:viewParam name="from" value="#{login.from}" /> </f:metadata> 

Then, when the real login succeeds, you can redirect to this URI to ExternalContext#redirect() :

 public void login() throws IOException { // ... FacesContext.getCurrentInstance().getExternalContext().redirect(from); } 

(if necessary, specify the default target for the case from null )

+10
source

Source: https://habr.com/ru/post/905267/


All Articles