Spring Boot错误401未经授权的模板

时间:2020-07-01 19:36:51

标签: spring spring-boot spring-security thymeleaf spring-security-oauth2

我有一个使用Spring Boot和旧版@EnableAuthorizationServer创建的OAuth 2.0授权服务器。以下是安全配置的摘要:

http
    .requestMatchers()
        .mvcMatchers("/login", "/profile", "/oauth/authorize", "/.well-known/jwks.json")
        .and()
    .authorizeRequests()
        .mvcMatchers("/login", "/.well-known/jwks.json").permitAll()
        .anyRequest().authenticated()
        .and()
    .formLogin()
        .loginPage("/login")
        .permitAll()

一切正常,现在我正在为错误消息创建Thymeleaf模板。在我的自定义页面上使用资源/模板/错误下的文件404.html显示了类似404之类的错误。对于任何未处理的异常,都有一个通用的resources / templates / error.html。

由于某些原因,未呈现模板401.html。相反,Spring会继续返回默认响应以及代码401和以下内容:

<oauth>
  <error_description>Full authentication is required to access this resource</error_description>
  <error>unauthorized</error>
</oauth>

您知道是否可以更改以及如何更改?为了清楚起见,我不想删除错误,而是返回自定义内容。

谢谢您的时间!

1 个答案:

答案 0 :(得分:0)

您应该告诉Spring Security您不想在错误页面上使用类似这样的验证:

http.authorizeRequests().mvcMatchers("/your/error/page/url").permitAll();