Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Security targetUrlParameter does not redirect

I'm trying to redirect the user back to the page where they clicked the login link. (Pages are read-only for non-authenticated users, but writable for logged in users.) How do I redirect the user back to where they came from after they login?

I'm sending to the user to the login page with this link: /spring_security_login?redirect=/item5. After loging in, I expect the user to be redirected to /item5 page. However, they're always redirected to / page.

Here is the configuration I'm using:

<http use-expressions="true">
    <intercept-url pattern="/**" access="permitAll" />
    <form-login authentication-success-handler-ref="simpleUrlAuthenticationSuccessHandler"/>
</http>
<beans:bean id="simpleUrlAuthenticationSuccessHandler"
    class="org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler">
    <beans:property name="defaultTargetUrl" value="/"/>
    <beans:property name="targetUrlParameter" value="redirect"/>
</beans:bean>

It seems that targetUrlParameter is not getting picked up as expected. I'm using Spring Security 3.1.4

like image 704
Victor Lyuboslavsky Avatar asked Jul 22 '13 03:07

Victor Lyuboslavsky


People also ask

How do I redirect in Spring Security?

By default, Spring Security will redirect after login to the secured ressource you tried to access. If you wish to always redirect to a specific URL, you can force that through the HttpSecurity configuration object. Assuming you are using a recent version of Spring Boot, you should be able to use JavaConfig.

How do I redirect a page in spring?

Try a URL http://localhost:8080/HelloWeb/index and you should see the following result if everything is fine with your Spring Web Application. Click the "Redirect Page" button to submit the form and to get the final redirected page.

What is loginProcessingUrl in Spring Security?

AND .loginProcessingUrl("/login/process") tells Spring Security to process the submitted credentials when sent the specified path and, by default, redirect user back to the page user came from. It will not pass the request to Spring MVC and your controller.


4 Answers

The following rules are applied when using the SimpleUrlAuthenticationSuccessHandler:

  • If the alwaysUseDefaultTargetUrl property is set to true, the defaultTargetUrl property will be used for the destination.
  • If a parameter matching the value of targetUrlParameter has been set on the request, the value will be used as the destination. By default this has the value "spring-security-redirect".
  • If the useReferer property is set, the "Referer" HTTP header value will be used, if present.
  • As a fallback option, the defaultTargetUrl value will be used.

According to your configuration, this should work. My guess is that you didn't propagate the referer when sending the POST request in the form login. Typically, you should write the referer value in an hidden field in your login page, so that the referer parameter is transmitted to spring_security_login.

like image 194
LaurentG Avatar answered Oct 01 '22 01:10

LaurentG


Change SimpleUrlAuthenticationSuccessHandler to SavedRequestAwareAuthenticationSuccessHandler and be happy.

like image 37
Rod Lima Avatar answered Oct 01 '22 01:10

Rod Lima


it because of this line :

<beans:property name="defaultTargetUrl" value="/"/>

remove that line and try again.

like image 43
Daniel Robertus Avatar answered Oct 01 '22 02:10

Daniel Robertus


Use SavedRequestAwareAuthenticationSuccessHandler instead of SimpleUrlAuthenticationSuccessHandler.

Even if the url of the page is typed on the browser in which case referer is not captured, SavedRequestAwareAuthenticationSuccessHandler uses the previous url captured by the ExceptionTraslationFilter.

Read http://docs.spring.io/spring-security/site/docs/3.0.x/reference/core-web-filters.html#form-login-flow-handling and http://docs.spring.io/autorepo/docs/spring-security/3.2.4.RELEASE/apidocs/org/springframework/security/web/authentication/SavedRequestAwareAuthenticationSuccessHandler.html

like image 25
Alexander Suraphel Avatar answered Oct 01 '22 00:10

Alexander Suraphel