spring - Spring Boot 错误 401 未经授权的模板
问题描述
我有一个使用 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 Security 你不想在错误页面上进行身份验证,如下所示:
http.authorizeRequests().mvcMatchers("/your/error/page/url").permitAll();
推荐阅读
- python - 将统计数据从百分比更改为事件数
- python - 每当访问 QTableWidget pyside 中的 Cell Item 时,QTableWidget.item 都是 noneType
- grep - grep 来自以“-”分隔的字符串的第三个关键字
- python - 带有 spyder 的熊猫风格
- r - makeForkCluster 和 nvblas(由 LD_PRELOAD 加载)
- powershell - 如何使用 powershell 命令将字符串替换为新行?从批处理文件中?- 双引号问题
- vue.js - 如果元素不存在,如何将元素添加到列表中,如果已经存在,如何将其删除
- javascript - 调用 useParams 时出错
- javascript - discord.js 使用嵌入回复
- php - 错误:未捕获的 PHP 异常 Doctrine\DBAL\DBALException