gpt4 book ai didi

java - Spring boot 未格式化的 BadCredentialsException

转载 作者:行者123 更新时间:2023-11-28 22:02:02 28 4
gpt4 key购买 nike

我有一个 Spring boot 配置的服务。通常我会在本地测试它,然后将其作为 .war 部署到 AWS ElasticBeansStalk 上的 Tomcat。

问题是:与我的开发环境(我在服务中嵌入了 Tomcat,spring boot 的默认设置)相比,AWS 抛出的 BadCredentialsException 的格式有所不同,例如:

如果我在本地抛出 BadCredentials 异常,我会收到 JSON 错误:

{
"timestamp": 1445622806489,
"status": 401,
"error": "Unauthorized",
"message": "Invalid user/password",
"path": "/user"
}

如果部署的服务遇到同样的错误,我会得到这个未格式化的困惑:

<!DOCTYPE html>
<html>
<head>
<title>Apache Tomcat/8.0.23 - Error report</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 - Invalid user/password</h1>
<div class="line"></div>
<p>
<b>type</b> Status report
</p>
<p>
<b>message</b>
<u>Invalid user/password</u>
</p>
<p>
<b>description</b>
<u>This request requires HTTP authentication.</u>
</p>
<hr class="line">
<h3>Apache Tomcat/8.0.23</h3>
</body>
</html>

有没有我可以用 Spring 做的配置,这样我就可以在所有错误消息上强制使用以前的格式,即使在导出到 .war 时也是如此?

最佳答案

这个问题是由ErrorPageFilter引起的,在嵌入式环境中不需要,但在非嵌入式环境中使用,因此部署到.war时可能会导致问题,具体取决于配置,如 this 中所述Spring Boot 的 github 上的错误报告。

该问题的一个解决方案是验证默认的 Spring Boot Security 设置是否已启用,删除任何 @EnableWebSecurity 或将 /error 添加到应用程序的排除路径.

关于java - Spring boot 未格式化的 BadCredentialsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33308782/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com