java - Spring boot 未格式化的 BadCredentialsException

标签 java spring tomcat spring-boot

我有一个 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/

相关文章:

java - 没有目的的 while 循环?

java - Eclipse,插件首选项加载问题

java - Spring Cache Evict 不起作用

java - EclipseLink 与 Spring - 无法持久化到 Db

java - Vaadin + Tomcat webapp 可以用来连续运行一个线程吗?

java - 在 Tomcat 8.5.4 源代码中进行更改

apache - 在 Apache Tomcat 中运行 Servlet

java - Java中default关键字的作用是什么?

java - Spring Boot 在 Debug模式下阻止 H2 控制台

java - 如何使用 Spring MVC 更新我当前的用户数据?