feat: OpenAPI 接口支持读取 Spring Security 白名单路径并跳过 Authorization 头,优化 Ant 风格路径匹配

This commit is contained in:
Ray.Hao
2025-01-13 18:09:30 +08:00
parent d9e25874ed
commit 6aa9d7b393
3 changed files with 44 additions and 43 deletions

View File

@@ -16,6 +16,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpHeaders;
import org.springframework.util.AntPathMatcher;
import java.util.stream.Stream;
@@ -82,10 +83,12 @@ public class OpenApiConfig {
if (openApi.getPaths() != null) {
openApi.getPaths().forEach((path, pathItem) -> {
// 忽略认证的请求无需携带Authorization
// 忽略认证的请求无需携带 Authorization
String[] ignoreUrls = securityProperties.getIgnoreUrls();
if (ArrayUtil.isNotEmpty(ignoreUrls)) {
if (Stream.of(ignoreUrls).anyMatch(path::equals)) {
// Ant 匹配忽略的路径不添加Authorization
AntPathMatcher antPathMatcher = new AntPathMatcher();
if (Stream.of(ignoreUrls).anyMatch(ignoreUrl -> antPathMatcher.match(ignoreUrl, path))) {
return;
}
}

View File

@@ -10,8 +10,8 @@ spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/youlai_boot?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&allowMultiQueries=true
username: root
url: jdbc:mysql://www.youlai.tech:3306/youlai_boot?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&allowMultiQueries=true
username: youlai
password: 123456
data:
redis:
@@ -87,8 +87,7 @@ security:
refresh-token-time-to-live: 604800
# 无需认证的请求路径
ignore-urls:
- /api/v1/auth/login # 用户登录接口
- /api/v1/auth/wechat-login # 微信登录接口
- /api/v1/auth/login/** # 登录接口(账号密码登录、手机验证码登录和微信登录)
- /api/v1/auth/captcha # 验证码获取接口
- /api/v1/auth/refresh-token # 刷新令牌接口
- /ws/** # WebSocket接口
@@ -141,18 +140,18 @@ sms:
regionId: cn-shanghai
signName: 有来技术
templates:
# 注册短信验证码模板
# 注册短信验证码模板
register: SMS_22xxx771
# 登录短信验证码模板
login: SMS_22xxx772
# 修改密码短信验证码模板
reset-password: SMS_22xxx773
# 修改手机号短信验证码模板
change-mobile: SMS_22xxx773
# springdoc配置 https://springdoc.org/properties.html
springdoc:
swagger-ui:
path: /swagger-ui.html
operationsSorter: alpha
operations-sorter: alpha
tags-sorter: alpha
api-docs:
path: /v3/api-docs
@@ -195,32 +194,32 @@ xxl:
# 验证码配置
captcha:
# 验证码类型 circle-圆圈干扰验证码|gif-Gif验证码|line-干扰线验证码|shear-扭曲干扰验证码
type: circle
# 验证码宽度
width: 120
# 验证码高度
height: 40
# 验证码干扰元素个数
interfere-count: 2
# 文本透明度(0.0-1.0)
text-alpha: 0.8
# 验证码字符配置
code:
# 验证码字符类型 math-算术|random-随机字符
type: math
# 验证码字符长度type=算术时,表示运算位数(1:个位数运算 2:十位数运算)type=随机字符时,表示字符个数
length: 1
# 验证码字体
font:
# 字体名称 Dialog|DialogInput|Monospaced|Serif|SansSerif
name: SansSerif
# 字体样式 0-普通|1-粗体|2-斜体
weight: 1
# 字体大小
size: 24
# 验证码有效期(秒)
expire-seconds: 120
# 验证码类型 circle-圆圈干扰验证码|gif-Gif验证码|line-干扰线验证码|shear-扭曲干扰验证码
type: circle
# 验证码宽度
width: 120
# 验证码高度
height: 40
# 验证码干扰元素个数
interfere-count: 2
# 文本透明度(0.0-1.0)
text-alpha: 0.8
# 验证码字符配置
code:
# 验证码字符类型 math-算术|random-随机字符
type: math
# 验证码字符长度type=算术时,表示运算位数(1:个位数运算 2:十位数运算)type=随机字符时,表示字符个数
length: 1
# 验证码字体
font:
# 字体名称 Dialog|DialogInput|Monospaced|Serif|SansSerif
name: SansSerif
# 字体样式 0-普通|1-粗体|2-斜体
weight: 1
# 字体大小
size: 24
# 验证码有效期(秒)
expire-seconds: 120
# 微信小程配置
wx:

View File

@@ -86,8 +86,7 @@ security:
refresh-token-time-to-live: 604800
# 无需认证的请求路径
ignore-urls:
- /api/v1/auth/login # 用户登录接口
- /api/v1/auth/wechat-login # 微信登录接口
- /api/v1/auth/login/** # 登录接口(账号密码登录、手机验证码登录和微信登录)
- /api/v1/auth/captcha # 验证码获取接口
- /api/v1/auth/refresh-token # 刷新令牌接口
- /ws/** # WebSocket接口
@@ -138,13 +137,13 @@ sms:
domain: dysmsapi.aliyuncs.com
regionId: cn-shanghai
signName: 有来技术
templateCodes:
# 注册(预留)
templates:
# 注册短信验证码模板
register: SMS_22xxx771
# 登录(预留)
# 登录短信验证码模板
login: SMS_22xxx772
# 修改密码
changePassword: SMS_22xxx773
# 修改手机号短信验证码模板
change-mobile: SMS_22xxx773
# springdoc配置 https://springdoc.org/properties.html
springdoc: