Spring security:注销后重定向到上一个网址

发布于 2024-11-09 10:31:13 字数 92 浏览 0 评论 0原文

我有一个使用 spring security 的网络应用程序。我想在用户注销时将用户重定向回他们注销之前所在的同一页面。

有没有简单的方法可以做到这一点?

I've got a web app that uses spring security. I'm wanting to redirect the user back to the same page they were on before log out when they log out.

Is there an easy way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

伴梦长久 2024-11-16 10:31:13

不确定这个问题指的是哪个 Spring 版本 - 但自 Spring 3.0 以来,标准 org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler 上有一个 useReferer 属性。

因此,您需要做的就是像这样配置它,注销将重定向到用户来自的任何地方:

<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>

Not sure which Spring version this question was referring to - but there is a useReferer property on the standard org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler since Spring 3.0.

So all you need to do is configure it like this and the logout will redirect to wherever the user came from:

<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>
◇流星雨 2024-11-16 10:31:13

您需要的是一个简单的 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"));
}

,然后在您的配置方法中调用它,即

.logout().logoutSuccessHandler(customLogoutSuccessHandler)

这会将您重定向到引用 URL。

What you need is a Simple 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 method i.e

.logout().logoutSuccessHandler(customLogoutSuccessHandler)

This will redirect you to referer URL.

情深已缘浅 2024-11-16 10:31:13

您可以在 Spring Security 的过滤器链中添加新的过滤器。该新过滤器将应用于 /logout URL。通过此过滤器时,您可以将当前页面保存在字段变量中。以及通过过滤器返回时。您可以将请求重定向到保存的 URL。我认为这会有所帮助。您可以使用 Request 对象中的 Referer 标头获取当前页面 URL。

You can add a new filter in the filter chain of the spring security. That new filter will be applied to the /logout URL. When going trough 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 can help. You can get the current page URL by using the Referer header in the Request object.

Hello爱情风 2024-11-16 10:31:13

只是一个想法:

我们保留一堆访问过的页面怎么样?最多可以有 3 个条目。并将用户从注销状态重定向。

  • 配置过滤器或扩展 Spring Security 过滤器以在会话中维护有关最近两个访问的 URL 的堆栈

  • 注销时将 servlet 配置为 logout-success-url

  • 现在从会话堆栈中获取 URL,立即 使会话无效并将用户重定向到该页面

您也可以使用 referrer header 正如 Vijay 提到的

Just a thought:

How about we keep a stack of visited pages. may be at max 3 entries. and redirects user from logout.

  • Configure a Filter or extend spring security filter to maintain a stack in session about last two visited URLs

  • On logout configure a servlet as logout-success-url .

  • Now get the URL from session stack and now invalidate the session and redirect user to that page

Also you can make use of referrer header as Vijay has mentioned

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文