Spring security:注销后重定向到上一个网址
我有一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不确定这个问题指的是哪个 Spring 版本 - 但自 Spring 3.0 以来,标准 org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler 上有一个 useReferer 属性。
因此,您需要做的就是像这样配置它,注销将重定向到用户来自的任何地方:
Not sure which Spring version this question was referring to - but there is a
useReferer
property on the standardorg.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:
您需要的是一个简单的 LogoutSuccessHandler
,然后在您的配置方法中调用它,即
这会将您重定向到引用 URL。
What you need is a Simple LogoutSuccessHandler
And later call it in your configure method i.e
This will redirect you to referer URL.
您可以在 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 theReferer
header in theRequest
object.只是一个想法:
我们保留一堆访问过的页面怎么样?最多可以有 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