Java 弹簧靴静止力响应类型/401自定义页面不工作?

Java 弹簧靴静止力响应类型/401自定义页面不工作?,java,spring,spring-boot,Java,Spring,Spring Boot,我正在使用SpringBoot2.2.0.RELEASE构建RESTAPI 我已经使用thymeleaf定制了500/400/404错误页面(如果有人使用浏览器导航),并将文件放入: src/main/resources/templates/error.html src/main/resources/templates/errors/400.html src/main/resources/templates/errors/404.html 这一切都很好 然而,我注意到,当有人在浏览器中导航到s

我正在使用SpringBoot2.2.0.RELEASE构建RESTAPI

我已经使用thymeleaf定制了500/400/404错误页面(如果有人使用浏览器导航),并将文件放入:

src/main/resources/templates/error.html
src/main/resources/templates/errors/400.html
src/main/resources/templates/errors/404.html
这一切都很好

然而,我注意到,当有人在浏览器中导航到say/myrestapi时,他们会得到一个401错误。默认情况下,在Spring中,他们会得到403错误,但我已经覆盖了该行为,使其成为所有401:

@Override
protected void configure(HttpSecurity http) throws Exception {
    // return HTTP 401 instead of HTTP 403 for unauthorized requests

    http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint());
}

private AuthenticationEntryPoint authenticationEntryPoint() {
    BasicAuthenticationEntryPoint basicAuthenticationEntryPoint = new BasicAuthenticationEntryPoint();
    basicAuthenticationEntryPoint.setRealmName("Bearer realm=\"oauth2-resource\"");
    return basicAuthenticationEntryPoint;
}
所以我补充说:

src/main/resources/templates/errors/401.html
这似乎没有得到重视,当我进入浏览器时,它显示XML:

<oauth>
<error_description>
Full authentication is required to access this resource
</error_description>
<error>unauthorized</error>
</oauth>
我的ResourceServerConfig.java如下所示:

@Override
public void configure(HttpSecurity http) throws Exception {
    // default behavior is to allow anonymous access unless @PreAuthorize is specified

http.exceptionHandling().accessDeniedPage("/resources/templates/errors/401.html").and().authorizeRequests().anyRequest()
                .permitAll();
}

但这也不行

我错过什么了吗

注意:我使用permitAll()让所有请求通过,然后使用@PreAuthorize在方法级别控制OAuth访问。但我也试着移除了permitAll(),但它仍然没有击中


还尝试了accessDeniedHandler,但也不起作用。

看起来您的路径是错误的。
应该是/resources/templates/errors/401.html

Ooops…C&p打字错误。我已经用一些额外的信息更新了这篇文章。如果这有什么不同的话,我也会大摇大摆。
@Override
public void configure(HttpSecurity http) throws Exception {
    // default behavior is to allow anonymous access unless @PreAuthorize is specified

http.exceptionHandling().accessDeniedPage("/resources/templates/errors/401.html").and().authorizeRequests().anyRequest()
                .permitAll();