Spring Security:如何获取初始目标 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/631217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Security: How to get the initial target url
提问by Spring Monkey
I am using the spring security to restricted urls. I am trying to provide signup and login page, on the same page.
我正在使用 spring 安全来限制网址。我正在尝试在同一页面上提供注册和登录页面。
On login spring security transfers to the restricted page. However i am trying to pass the target url to the signup process, so that after signup we can redirect to the restricted page.
在登录 spring 安全转移到受限页面。但是我试图将目标 url 传递给注册过程,以便在注册后我们可以重定向到受限页面。
How to get the actual URL that user was redirected from.
如何获取用户重定向的实际 URL。
Any Ideas?
有任何想法吗?
采纳答案by Spring Monkey
This is how i got the URL from the Spring Security.
这就是我从 Spring Security 获取 URL 的方式。
SavedRequest savedRequest = (SavedRequest)session.getAttribute(
AbstractProcessingFilter.SPRING_SECURITY_SAVED_REQUEST_KEY);
String requestUrl = savedRequest.getFullRequestUrl();
回答by stevevls
They moved things around a bit in spring security 3.0, so the above code snippet doesn't work anymore. This does the trick, though:
他们在 spring security 3.0 中稍微移动了一些东西,所以上面的代码片段不再起作用。不过,这确实有效:
protected String getRedirectUrl(HttpServletRequest request) {
HttpSession session = request.getSession(false);
if(session != null) {
SavedRequest savedRequest = (SavedRequest) session.getAttribute(WebAttributes.SAVED_REQUEST);
if(savedRequest != null) {
return savedRequest.getRedirectUrl();
}
}
/* return a sane default in case data isn't there */
return request.getContextPath() + "/";
}
回答by Dreal
with spring security 4.1.4:
使用弹簧安全 4.1.4:
@Override
public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
SavedRequest savedRequest = new HttpSessionRequestCache().getRequest(request, response);
if (savedRequest != null) {
response.sendRedirect(savedRequest.getRedirectUrl());
}
else{
response.sendRedirect("some/path");
}
}

