#
我正在使用 Spring Security 并且想知道如果该页面包含(哈希)符号,在成功登录到源页面后如何实现重定向。
现在我使用always-use-default-target="false"
它,它在 URL 类型上运行良好:/path/to/page/
.
但是当 URL 变为#/path/to/page
它时,它不会进行任何重定向。
有什么办法可以解决吗?
#
我正在使用 Spring Security 并且想知道如果该页面包含(哈希)符号,在成功登录到源页面后如何实现重定向。
现在我使用always-use-default-target="false"
它,它在 URL 类型上运行良好:/path/to/page/
.
但是当 URL 变为#/path/to/page
它时,它不会进行任何重定向。
有什么办法可以解决吗?
这是我最后使用的解决方案:
$(document).ready(function(){
$('#auth-form').submit(function() {
var el = $(this);
var hash = window.location.hash;
if (hash) el.prop('action', el.prop('action') + '#' + unescape(hash.substring(1)));
return true;
});
});
此片段将哈希添加到授权表单的 action 属性中,Spring 将您重定向到 kind: 的 URL,#/path/to/page
没有任何问题。
也许这是一个老问题,但在我最近对该主题的研究中,我发现这个问题很常见并且仍然存在(尤其是在具有后端安全性的现代 AngularJS 前端应用程序的情况下)。我想和你分享我的解决方案。
在登录页面,例如 /login.html,在标签前添加以下代码</body>
:
<script type="text/javascript">
var hash = window.location.hash;
document.cookie="hashPart=" + window.btoa(hash);
</script>
注意 (1): btoa() 函数在 IE >= 10 ( http://www.w3schools.com/jsref/met_win_btoa.asp ) 中工作,对于旧版浏览器使用 jQuery 等效项。
注(2):URL#部分的加密是必要的,因为它可能包含特殊字符,不允许存储在cookie值字符串中。
从服务器端,您必须修改onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
类实现AuthenticationSuccessHandler
接口的方法。
就我而言,我只是扩展SavedRequestAwareAuthenticationSuccessHandler
类并onAuthenticationSuccess
使用其原始代码覆盖该方法。然后我从请求中获取hashPart cookie 值,对其进行解码并添加到已解析的重定向 URL。我的代码片段如下:
@Override
public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication)
throws ServletException, IOException {
// ... copy/paste original implementation here, until ...
// Use the DefaultSavedRequest URL
String targetUrl = savedRequest.getRedirectUrl();
for (Cookie cookie : req.getCookies()) {
if (cookie.getName().equals("hashPart")) {
targetUrl += new String(Base64Utils.decodeFromString(cookie.getValue()));
cookie.setMaxAge(0); // clear cookie as no longer needed
response.addCookie(cookie);
break;
}
}
getRedirectStrategy().sendRedirect(request, response, targetUrl);
}
最后,只需将您的成功处理程序类注入您的 Spring Security 配置,如下所述:https ://stackoverflow.com/a/21100458/3076403
我期待您对此问题的评论或其他解决方案。