如何在 IIS 服务器中禁用自定义错误页面生成

How to disable custom error page generation in IIS server

我的 Spring Boot 应用程序 运行 在 IIS Server 上生成自定义错误页面用于在 header 处查找错误代码 401 的远程请求。我想禁用它。响应数据中不应有自定义错误页面。 Apache server 中的默认行为是期望的行为。虽然 IIS Server 可以配置为防止为远程请求生成自定义错误页面,但我希望它从我的应用程序配置(可能是 Web 配置点)。 Spring 可以吗?

在您的 web.xml 中添加:

<error-page>
    <error-code>401</error-code>
    <location>/errors/unauthorised</location>
</error-page>

然后像这样添加一个控制器:

@Controller
@RequestMapping("/errors")
public class ApplicationExceptionHandler {

    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @RequestMapping("unauthorised")
    public ModelAndView unauthorizedError(){
        return new ModelAndView("errors/error.jsp");
    }
}

web.config 文件添加到应用程序的 webapp 目录 (app/src/main/webapp)web.config 文件应包含:

<configuration>
   <system.webServer>
      <httpErrors errorMode="Detailed" existingResponse="Auto" />
   </system.webServer>
</configuration>

这将导致 IIS web server 将错误模式设置为 'Detailed',这将阻止生成自定义错误页面。