swagger上选择的接口和实际接口不对应 #8705

springboot3_sas^2
JEECG 2025-08-13 17:22:42 +08:00
parent 6b7542620b
commit 8cc033b86f
1 changed files with 42 additions and 3 deletions

View File

@ -16,6 +16,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@ -64,21 +65,59 @@ public class Swagger3Config implements WebMvcConfigurer {
@Bean @Bean
public OperationCustomizer operationCustomizer() { public OperationCustomizer operationCustomizer() {
return (operation, handlerMethod) -> { return (operation, handlerMethod) -> {
String path = handlerMethod.getBeanType().getAnnotation(RequestMapping.class).value()[0]; String path = getFullPath(handlerMethod);
if (!excludedPaths.contains(path)) { if (!isExcludedPath(path)) {
operation.addSecurityItem(new SecurityRequirement().addList(CommonConstant.X_ACCESS_TOKEN)); operation.addSecurityItem(new SecurityRequirement().addList(CommonConstant.X_ACCESS_TOKEN));
}else{
log.info("忽略加入 X_ACCESS_TOKEN 的 PATH:" + path);
} }
return operation; return operation;
}; };
} }
private String getFullPath(HandlerMethod handlerMethod) {
StringBuilder fullPath = new StringBuilder();
// 获取类级别的路径
RequestMapping classMapping = handlerMethod.getBeanType().getAnnotation(RequestMapping.class);
if (classMapping != null && classMapping.value().length > 0) {
fullPath.append(classMapping.value()[0]);
}
// 获取方法级别的路径
RequestMapping methodMapping = handlerMethod.getMethodAnnotation(RequestMapping.class);
if (methodMapping != null && methodMapping.value().length > 0) {
String methodPath = methodMapping.value()[0];
// 确保路径正确拼接,处理斜杠
if (!fullPath.toString().endsWith("/") && !methodPath.startsWith("/")) {
fullPath.append("/");
}
fullPath.append(methodPath);
}
return fullPath.toString();
}
private boolean isExcludedPath(String path) {
return excludedPaths.stream()
.anyMatch(pattern -> {
if (pattern.endsWith("/**")) {
// 处理通配符匹配
String basePath = pattern.substring(0, pattern.length() - 3);
return path.startsWith(basePath);
}
// 精确匹配
return pattern.equals(path);
});
}
@Bean @Bean
public OpenAPI customOpenAPI() { public OpenAPI customOpenAPI() {
return new OpenAPI() return new OpenAPI()
.info(new Info() .info(new Info()
.title("JeecgBoot 后台服务API接口文档") .title("JeecgBoot 后台服务API接口文档")
.version("3.8.1") .version("3.8.2")
.contact(new Contact().name("北京国炬信息技术有限公司").url("www.jeccg.com").email("jeecgos@163.com")) .contact(new Contact().name("北京国炬信息技术有限公司").url("www.jeccg.com").email("jeecgos@163.com"))
.description("后台API接口") .description("后台API接口")
.termsOfService("NO terms of service") .termsOfService("NO terms of service")