【问题标题】:http error handling using ErrorController使用 ErrorController 处理 http 错误
【发布时间】:2021-10-11 17:07:15
【问题描述】:

我正在使用带有 AuthenticationEntryPoint 的 httpBasic 来验证此人是否已通过身份验证。我想通过 ErrorOcntroller 接口发送自定义错误页面。而不是仅仅返回一个字符串。

这是我的代码:

@Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
        response.addHeader("WWW-Authenticate", "Basic realm=" + getRealmName());
        response.response.sendRedirect("/error");
    }

我的错误控制器类

@Controller
public class RequestErrorController implements ErrorController {

    private static final Logger log = LoggerFactory.getLogger(RequestErrorController.class);
    @RequestMapping("/error")
    public String handleError(HttpServletRequest request) {
        Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);

        //log.error("Error 404 generated for this request ->"+ request.getRequestURI());
        if (status != null) {
            int statusCode = Integer.parseInt(status.toString());

            if(statusCode == HttpStatus.UNAUTHORIZED.value()) {
                return "error-401";
            }
            if(statusCode == HttpStatus.NOT_FOUND.value()) {
                return "error-404";
            }
            else if(statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
                return "error-403";
            }
            else if(statusCode == HttpStatus.BAD_REQUEST.value()) {
                return "The request did not match any end-point. Please revise the request signature and parameters type. ";
            }
            // on va rajouter bad request vue que 500 est deja la
        }
        return "error";
    }

    @Override
    public String getErrorPath() {
        return "/error";
    }
}

问题是它根本没有到达这个控制器。

我已经对我的代码进行了更新,并最终获得了控制器。 现在的问题是,它正在寻找freemake模板:

2021-08-06 18:03:42,240 INFO [http-nio-8080-exec-2] c.n.j.A.RequestInterceptor [RequestInterceptor.java:122] returning true
0 [http-nio-8080-exec-2] DEBUG freemarker.cache  - Couldn't find template in cache for "error.ftlh"("en_US", UTF-8, parsed); will try to load it.
2 [http-nio-8080-exec-2] DEBUG freemarker.cache  - TemplateLoader.findTemplateSource("error_en_US.ftlh"): Not found
3 [http-nio-8080-exec-2] DEBUG freemarker.cache  - TemplateLoader.findTemplateSource("error_en.ftlh"): Not found
3 [http-nio-8080-exec-2] DEBUG freemarker.cache  - TemplateLoader.findTemplateSource("error.ftlh"): Not found
2021-08-06 18:03:42,272 DEBUG [http-nio-8080-exec-2] o.s.w.f.CommonsRequestLoggingFilter [CommonsRequestLoggingFilter.java:55] REQUEST DATA : GET /error]

【问题讨论】:

  • 可能与@ResponseBody 关联到handleError
  • 它没有到达那里。

标签: java spring spring-boot freemarker


【解决方案1】:

我发现了一个类似的堆栈讨论 https://stackoverflow.com/a/26221980/16571215

那里似乎有另一种解决方案。

【讨论】:

    【解决方案2】:

    关于如何创建自定义错误页面的说明 https://www.baeldung.com/spring-boot-custom-error-page 他们只是使用普通的 .HTML 页面

    您可能在项目中使用了 freemaker 依赖项:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>**spring-boot-starter-freemarker**</artifactId>
    </dependency>
    

    您是否尝试在资源文件夹中创建 error.ftlh、error-401.ftlh、error-403.ftlh 和 error-404.ftlh 模板?

    【讨论】:

    • 我确实使用 freemake。我不想为自定义错误页面免费制作。
    • 我明白了。您是否尝试过将错误页面创建为 HTML 文件,例如 error.html、error-404.html 等?也许如果您提供 HTML 页面,那么 freemaker 将不会被使用?
    猜你喜欢
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    • 2016-08-06
    • 2016-09-20
    • 2012-11-16
    相关资源
    最近更新 更多