Spring security: redirect to previous url after logout

I have a web application using spring security. I want to redirect the user to the same page that they were on before logging out when they log out.

Is there an easy way to do this?

+5
source share
4 answers

You can add a new filter to the spring security filter chain. This new file will be applied to the URL /logout. When passing through this filter, you can save the current page in a field variable. And when returning through the filter. You can redirect the request to the saved URL. I think this may help. You can get the current page URL with. Refererin the facility Request.

+1
source

You do not know which Spring variant this question relates to, but in the standard org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandlerthere is a property useReferer, since Spring 3.0.

So, all you have to do is configure it like this, and the output will be redirected to where the user came:

<bean id="logoutSuccessHandler" class="org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler">
    <property name="useReferer" value="true"/>
</bean>

<security:http>
    <security:logout logout-url="/logout" success-handler-ref="logoutSuccessHandler" />
</security:http>
+7
source

:

, . 3 . .

  • spring, URL.

  • logout-success-url.

  • URL-

referrer header, Vijay

0

LogoutSuccessHandler

@Component
  public class CustomLogoutSuccessHandler extends 
  SimpleUrlLogoutSuccessHandler implements LogoutSuccessHandler {

@Override
 public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse 
     response, Authentication authentication)
        throws IOException, ServletException {
    if (authentication != null) {
        System.out.println(authentication.getName());
    }
    response.setStatus(HttpStatus.OK.value());
    response.sendRedirect(request.getHeader("referer"));
}

And later call it in your configure ie method

.logout().logoutSuccessHandler(customLogoutSuccessHandler)

This will redirect you to the referrer URL.

0
source

All Articles