我正在尝试使用 Spring Boot
编写身份验证过滤器;其中我检查一些 cookie,如果它存在于请求中,我想将请求标记为已授权,如果不是,我想返回 401
header 以响应客户端调用我的服务。
下面是我在 Spring Boot 过滤器中使用的行:
((HttpServletResponse) res).sendError(HttpServletResponse.SC_UNAUTHORIZED, "User is not authenticated, so can not access IPT service.");
虽然当请求中没有 cookie 时,这行代码会在我的 Auth 过滤器中执行;在我的客户端代码中,当使用 response.headers
401
设置
我什至尝试使用:
((HttpServletResponse) res).addHeader("401", "Unauthorized!");
return;
但是,运气不好!
我做错了什么吗?
编辑#1:
添加处理请求/响应的客户端代码:
fetch(FETCH_URL, {
method: 'GET',
dataType: 'json',
credentials: 'include'
}).then(
function(response) {
if(response.status == 401) {
console.log("----->", response);
alert(response + " Redirect to login page!");
}
},
function() {
alert("Error!");
});
编辑#2:
当我尝试按照 Finnbar O'G 的建议获取错误消息时:
response.text().then((text) => { console.log("----->", text); });
我得到了完整的 HTML:
<!doctype html>
<html lang="en">
<head><title>HTTP Status 401 – Unauthorized</title>
<style type="text/css">h1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} h2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} h3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} b {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;} a {color:black;} a.name {color:black;} .line {height:1px;background-color:#525D76;border:none;}
</style>
</head>
<body>
<h1>HTTP Status 401 – Unauthorized</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Message</b> User is not authenticated, so can not access IPT service.</p><p><b>Description</b> The request has not been applied because it lacks valid authentication credentials for the target resource.</p><hr class="line" /><h3>Apache Tomcat/9.0.0.M22</h3>
</body>
</html>
我怎样才能只从我添加为我的 sendError
方法的第二个参数的服务器获取消息?
好吧,这是我的过滤器代码:
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
LOGGER.info("****************** Inside Auth Filter ******************");
boolean success = process(req); // Checks if auth cookie is set in request
if (success) {
filterChain.doFilter(req, res);
} else {
// ((HttpServletResponse) res).addHeader("401", "Unauthorized!");
((HttpServletResponse) res).sendError(HttpServletResponse.SC_UNAUTHORIZED, "User is not authenticated, so can not access IPT service.");
}
}
谢谢
最佳答案
response.headers 将只包含设置的 HTTP header ,sendError 正在发送一个完整的 HTTP 响应,其中包括 header 、响应正文和状态。 response.status 将是 401 错误,response.body 将是消息“用户未通过身份验证,因此无法访问 IPT 服务。”。您应该在您的客户端代码中检查这些。
编辑:
由于您使用 fetch(),您可以使用 response.text() 访问响应正文作为普通的 tet。这将返回一个 Promise,在您可以使用响应主体之前必须先解决该 Promise:例如
fetch(FETCH_URL, {
method: 'GET',
dataType: 'json',
credentials: 'include'
}).then(
function(response) {
if(response.status == 401) {
response.text().then((text) => { console.log("----->", text); });
alert(response + " Redirect to login page!");
}
},
function() {
alert("Error!");
});
关于使用 fetch 的 MDN 文档对于理解如何处理响应很有帮助。
关于javascript - 无法从 Spring Boot 过滤器在 http header 中发送 "Unauthorized",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45206058/