Redirect to protected resource or original / saved request after checking servlet 3.0 HttpServletRequest # login ()?

As expected, the login page loads when a secure / secure resource is requested:

<login-config> <auth-method>FORM</auth-method> <realm-name>jdbc</realm-name> <form-login-config> <form-login-page>/login.xhtml</form-login-page> <form-error-page>/login.xhtml</form-error-page> </form-login-config> </login-config> 

I understand that j_security_check will automatically switch to a secure / secure resource if authentication is successful:

 <form method="post" action="j_security_check"> <input type="text" name="j_username"> <input type="password" name= "j_password"> </form> 

However, I would like users to register (or login) to continue, so I used JSF 2.0: <h:form... , EL: #{loginBean.register()}... etc ... and I am doing authentication programmatically using Servlet 3.0:

 public void register() { HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(); try { // Register... request.login(this.username, this.password); // Redirect to the protected/secure resource... } catch (ServletException e) { ... } } 

How to find out what the resource originally requested? Maybe:

  • Get a "saved request" from a session (specific container)?
  • Try in some way to access the "source request"? ()
  • Anything related to the query manager (wild guess)?
  • Use the header "referer" (bad idea)?
  • Create a server authentication module (SAM) (not easy)?

Any advice would be greatly appreciated!

+4
source share
1 answer

The login page is under the covers opened by redirects, and the original request URI is available as a request attribute named javax.servlet.forward.request_uri .

So:

 String uri = request.getAttribute("javax.servlet.forward.request_uri"); 

or, more JSF-ish:

 String uri = externalContext.getRequestMap().get("javax.servlet.forward.request_uri"); 
+5
source

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


All Articles