帖子是否始终使用重定向来响应请求。如果是这样,我如何确保响应头也被转发到重定向的URL?目前,我在我发送的响应标头中设置了JWT令牌,但重定向的URL不包含令牌。有人能告诉我如何确保获得JWT令牌以便我可以在我的进一步请求中使用它。
String token = JWT.create()
.withSubject(((LdapUserDetails) authentication.getPrincipal()).getUsername())
.withExpiresAt(new Date(System.currentTimeMillis() + EXPIRATION_TIME))
.sign(HMAC512(SECRET.getBytes()));
response.addHeader(HEADER_STRING, TOKEN_PREFIX + token);
Object redirectURLObject = request.getSession().getAttribute(REDIRECT_URL_SESSION_ATTRIBUTE_NAME);
if(redirectURLObject != null)
setDefaultTargetUrl(redirectURLObject.toString());
else{
setDefaultTargetUrl("http://localhost:8000");
}
request.getSession().removeAttribute(REDIRECT_URL_SESSION_ATTRIBUTE_NAME);
super.onAuthenticationSuccess(request, response, authentication);
如果你确定super.onAuthenticationSuccess(request, response, authentication);
线,它很可能是罪魁祸首。
我认为你的班级就像extends
AbstractAuthenticationTargetUrlRequestHandler implements
AuthenticationSuccessHandler {
如果你深入挖掘,你会发现在handle
类中的AbstractAuthenticationTargetUrlRequestHandler
方法中有这一行:
redirectStrategy.sendRedirect(request, response, targetUrl);
将从您的浏览器重定向302状态。
如果您了解此问题,则无法将其置于标题中。还有其他方法:您可以从前端进行AJAX调用,或者在标头中使用JWT返回JSON,或者在没有sendRedirect的情况下进行转发。
可能是你正在寻求转发请求(RequestDispatcher.forward() vs HttpServletResponse.sendRedirect())