chore(swagger): 移除Swagger相关配置和依赖

This commit is contained in:
2025-10-30 17:15:26 +08:00
parent 8e17eb1297
commit 6d28669921
4 changed files with 98 additions and 121 deletions
@@ -47,8 +47,10 @@ public class BootApplication {
env.getProperty("config.servlet.context-path"); env.getProperty("config.servlet.context-path");
log.info( log.info(
"\n----------------------------------------------------------\n\t" + "\n----------------------------------------------------------\n\t" +
"Application is running! Access URLs:\n\t" + "Local: \t\thttp://localhost:" + port + path + "/\n\t" + "Application is running! Access URLs:\n\t" +
"External: \thttp://" + ip + ":" + port + path + "/\n\t" + "Swagger文档: \thttp://" + ip + ":" + port + "Local: \t\thttp://localhost:" + port + path + "/\n\t" +
path + "/doc.html\n" + "----------------------------------------------------------"); "External: \thttp://" + ip + ":" + port + path + "/\n\t"
// + "Swagger文档: \thttp://" + ip + ":" + port + path + "/doc.html\n"
+ "----------------------------------------------------------");
} }
} }
@@ -1,48 +1,48 @@
/* ///*
* Copyright 2021-2025 Odboy // * Copyright 2021-2025 Odboy
* // *
* Licensed under the Apache License, Version 2.0 (the "License"); // * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. // * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at // * You may obtain a copy of the License at
* // *
* http://www.apache.org/licenses/LICENSE-2.0 // * http://www.apache.org/licenses/LICENSE-2.0
* // *
* Unless required by applicable law or agreed to in writing, software // * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, // * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and // * See the License for the specific language governing permissions and
* limitations under the License. // * limitations under the License.
*/ // */
//
package cn.odboy.framework.doc; //package cn.odboy.framework.doc;
//
import cn.odboy.framework.properties.AppProperties; //import cn.odboy.framework.properties.AppProperties;
import io.swagger.v3.oas.models.Components; //import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI; //import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info; //import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement; //import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme; //import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.beans.factory.annotation.Autowired; //import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; //import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; //import org.springframework.context.annotation.Configuration;
//
/** ///**
* api页面 /doc.html // * api页面 /doc.html
*/ // */
@Configuration //@Configuration
public class SwaggerConfig { //public class SwaggerConfig {
@Autowired // @Autowired
private AppProperties properties; // private AppProperties properties;
//
@Bean // @Bean
public OpenAPI customOpenAPI() { // public OpenAPI customOpenAPI() {
// 添加开关控制 // // 添加开关控制
if (!properties.getSwagger().getEnabled()) { // if (!properties.getSwagger().getEnabled()) {
return new OpenAPI(); // return new OpenAPI();
} // }
return new OpenAPI().components(new Components().addSecuritySchemes("Authorization", // return new OpenAPI().components(new Components().addSecuritySchemes("Authorization",
new SecurityScheme().name("Authorization").type(SecurityScheme.Type.APIKEY).in(SecurityScheme.In.HEADER))) // new SecurityScheme().name("Authorization").type(SecurityScheme.Type.APIKEY).in(SecurityScheme.In.HEADER)))
.info(new Info().title("CuteJava 接口文档").version("1.4.1").description("一个简单且易上手的自动化运维平台")) // .info(new Info().title("CuteJava 接口文档").version("1.4.1").description("一个简单且易上手的自动化运维平台"))
.addSecurityItem(new SecurityRequirement().addList("Authorization")); // .addSecurityItem(new SecurityRequirement().addList("Authorization"));
} // }
} //}
@@ -80,66 +80,41 @@ public class SpringSecurityConfig {
TokenFilter customFilter = new TokenFilter(tokenProvider, systemUserOnlineInfoDAO); TokenFilter customFilter = new TokenFilter(tokenProvider, systemUserOnlineInfoDAO);
return httpSecurity return httpSecurity
// 禁用 CSRF // 禁用 CSRF
.csrf(AbstractHttpConfigurer::disable) .csrf(AbstractHttpConfigurer::disable).addFilter(corsFilter).addFilterBefore(customFilter, UsernamePasswordAuthenticationFilter.class)
.addFilter(corsFilter) // 授权异常
.addFilterBefore(customFilter, UsernamePasswordAuthenticationFilter.class) .exceptionHandling(
// 授权异常 exceptionHandling -> exceptionHandling.authenticationEntryPoint(authenticationErrorHandler).accessDeniedHandler(jwtAccessDeniedHandler))
.exceptionHandling(exceptionHandling -> exceptionHandling // 防止iframe 造成跨域
.authenticationEntryPoint(authenticationErrorHandler) .headers(headers -> headers.frameOptions(HeadersConfigurer.FrameOptionsConfig::disable))
.accessDeniedHandler(jwtAccessDeniedHandler)) // 不创建会话
// 防止iframe 造成跨域 .sessionManagement(sessionManagement -> sessionManagement.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.headers(headers -> headers .authorizeHttpRequests(authorizeRequests -> authorizeRequests
.frameOptions(HeadersConfigurer.FrameOptionsConfig::disable)) // 静态资源等等
// 不创建会话 .requestMatchers(HttpMethod.GET, "*.html", "*.css", "*.js", "/websocket/**").permitAll()
.sessionManagement(sessionManagement -> sessionManagement // // Knife4j 和 OpenAPI 相关路径
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) // .requestMatchers("/doc.html", "/webjars/**", "/v3/api-docs", "/v3/api-docs/**", "/swagger-resources", "/swagger-resources/**",
.authorizeHttpRequests(authorizeRequests -> authorizeRequests // "/configuration/ui", "/configuration/security").permitAll()
// 静态资源等等 // 文件
.requestMatchers(HttpMethod.GET, "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/websocket/**") .requestMatchers("/avatar/**").permitAll().requestMatchers("/file/**").permitAll()
.permitAll() // 阿里巴巴 druid
// swagger 文档 .requestMatchers("/druid/**").permitAll()
.requestMatchers("/swagger-ui.html") // 放行OPTIONS请求
.permitAll() .requestMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.requestMatchers("/swagger-resources/**") // 自定义匿名访问所有url放行:允许匿名和带Token访问,细腻化到每个 Request 类型
.permitAll() // GET
.requestMatchers("/webjars/**") .requestMatchers(HttpMethod.GET, anonymousUrls.get(RequestMethodEnum.GET.getType()).toArray(new String[0])).permitAll()
.permitAll() // POST
.requestMatchers("/*/api-docs") .requestMatchers(HttpMethod.POST, anonymousUrls.get(RequestMethodEnum.POST.getType()).toArray(new String[0])).permitAll()
.permitAll() // PUT
// 文件 .requestMatchers(HttpMethod.PUT, anonymousUrls.get(RequestMethodEnum.PUT.getType()).toArray(new String[0])).permitAll()
.requestMatchers("/avatar/**") // PATCH
.permitAll() .requestMatchers(HttpMethod.PATCH, anonymousUrls.get(RequestMethodEnum.PATCH.getType()).toArray(new String[0])).permitAll()
.requestMatchers("/file/**") // DELETE
.permitAll() .requestMatchers(HttpMethod.DELETE, anonymousUrls.get(RequestMethodEnum.DELETE.getType()).toArray(new String[0])).permitAll()
// 阿里巴巴 druid // 所有类型的接口都放行
.requestMatchers("/druid/**") .requestMatchers(anonymousUrls.get(RequestMethodEnum.ALL.getType()).toArray(new String[0])).permitAll()
.permitAll() // 所有请求都需要认证
// 放行OPTIONS请求 .anyRequest().authenticated()).build();
.requestMatchers(HttpMethod.OPTIONS, "/**")
.permitAll()
// 自定义匿名访问所有url放行:允许匿名和带Token访问,细腻化到每个 Request 类型
// GET
.requestMatchers(HttpMethod.GET, anonymousUrls.get(RequestMethodEnum.GET.getType()).toArray(new String[0]))
.permitAll()
// POST
.requestMatchers(HttpMethod.POST, anonymousUrls.get(RequestMethodEnum.POST.getType()).toArray(new String[0]))
.permitAll()
// PUT
.requestMatchers(HttpMethod.PUT, anonymousUrls.get(RequestMethodEnum.PUT.getType()).toArray(new String[0]))
.permitAll()
// PATCH
.requestMatchers(HttpMethod.PATCH, anonymousUrls.get(RequestMethodEnum.PATCH.getType()).toArray(new String[0]))
.permitAll()
// DELETE
.requestMatchers(HttpMethod.DELETE, anonymousUrls.get(RequestMethodEnum.DELETE.getType()).toArray(new String[0]))
.permitAll()
// 所有类型的接口都放行
.requestMatchers(anonymousUrls.get(RequestMethodEnum.ALL.getType()).toArray(new String[0]))
.permitAll()
// 所有请求都需要认证
.anyRequest()
.authenticated())
.build();
} }
} }
+7 -7
View File
@@ -154,19 +154,19 @@
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
</dependency> </dependency>
<!--监控sql日志--> <!-- 监控sql日志 -->
<dependency> <dependency>
<groupId>p6spy</groupId> <groupId>p6spy</groupId>
<artifactId>p6spy</artifactId> <artifactId>p6spy</artifactId>
<version>3.9.1</version> <version>3.9.1</version>
</dependency> </dependency>
<!-- Swagger UI 相关 --> <!-- Spring Boot 3 只支持OpenAPI3规范,暂时不接 -->
<dependency> <!-- <dependency>-->
<groupId>com.github.xiaoymin</groupId> <!-- <groupId>com.github.xiaoymin</groupId>-->
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId> <!-- <artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>-->
<version>4.5.0</version> <!-- <version>4.4.0</version>-->
</dependency> <!-- </dependency>-->
<dependency> <dependency>
<groupId>io.swagger</groupId> <groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId> <artifactId>swagger-annotations</artifactId>