【问题标题】:Template for Spring Boot error 401 UnauthorizedSpring Boot 错误 401 未经授权的模板
【发布时间】:2020-10-22 08:02:42
【问题描述】:

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

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

感谢您的宝贵时间!

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 您好 Fabien,感谢您的回答。但是,这并没有真正的帮助。首先,错误页面没有 URL 这样的东西,它们不是端点。 Spring 将在适当的时候生成例如 404 或 401 响应,但不会真正将用户重定向到另一个 URL。
    猜你喜欢
    • 1970-01-01
    • 2022-01-21
    • 2016-02-10
    • 1970-01-01
    • 2021-04-07
    • 2020-12-09
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多