SpringBoot整合Swagger頁面禁止訪問swagger-ui.html方式
SpringBoot整合Swagger頁面禁止訪問swagger-ui.html
在Spring Boot中禁止訪問Swagger UI頁面并在攔截器中進行攔截可以通過配置Spring Security來實現(xiàn)。
下面是一個簡單的示例,演示如何實現(xiàn)這一點:
在Spring Boot項目中創(chuàng)建一個Spring Security配置類
如下所示:
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/swagger-ui.html").denyAll()
.antMatchers("/swagger-resources/**").permitAll() // 如果需要訪問Swagger的其他資源,可以放行
.and()
.csrf().disable();
}
}在這個配置中,我們使用HttpSecurity對象配置了訪問規(guī)則。
.antMatchers("/swagger-ui.html").denyAll()表示禁止訪問swagger-ui.html頁面- 而
.antMatchers("/swagger-resources/**").permitAll()則允許訪問Swagger的其他資源
創(chuàng)建一個攔截器(Interceptor)類
用于攔截對 swagger-ui.html 的訪問:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (request.getRequestURI().equals("/swagger-ui.html")) {
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
return false; // 攔截訪問
}
return true; // 放行其他請求
}
// 可以實現(xiàn) postHandle 和 afterCompletion 方法進行相應處理
}配置這個攔截器類并使其生效:
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor());
}
}這樣配置后,即可通過Spring Security和攔截器實現(xiàn)禁止訪問Swagger UI頁面 swagger-ui.html。
如果你想完全禁用Swagger UI和Swagger資源
你可以在 Spring Boot 項目的 application.yml 或 application.properties 文件中添加以下配置來實現(xiàn):
- 在
application.yml文件中的配置:
spring:
profiles:
swagger:
enabled: false- 在
application.properties文件中的配置:
spring.profiles.swagger.enabled=false
通過將這些配置設置為 false,你可以完全禁用 Spring Boot 中關于 Swagger UI 和 Swagger 資源的自動配置和展示。
這樣就可以確保這些端點和頁面對外部用戶不可見或無法訪問。
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
java http連接池的實現(xiàn)方式(帶有失敗重試等高級功能)
這篇文章主要介紹了java http連接池的實現(xiàn)方式(帶有失敗重試等高級功能),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04
springboot CompletableFuture并行計算及使用方法
CompletableFuture基于 Future 和 CompletionStage 接口,利用線程池、回調函數(shù)、異常處理、組合操作等機制,提供了強大而靈活的異步編程功能,這篇文章主要介紹了springboot CompletableFuture并行計算及使用方法,需要的朋友可以參考下2024-05-05
Spring?Boot中MongoTemplate從入門到實戰(zhàn)深度解析
本文介紹了Spring?Data MongoDB中的MongoTemplate,包括主要特性、核心配置、基礎CRUD、高級查詢與聚合操作、GridFS操作、性能優(yōu)化與監(jiān)控以及最佳實踐,通過全面的示例項目,展示了如何在Spring?Boot應用中使用MongoTemplate進行數(shù)據(jù)庫操作,感興趣的朋友跟隨小編一起看看吧2026-01-01
spring boot下mybatis配置雙數(shù)據(jù)源的實例
這篇文章主要介紹了spring boot下mybatis配置雙數(shù)據(jù)源的實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
java8 stream 如何打印數(shù)據(jù)元素
這篇文章主要介紹了java8 stream 如何打印數(shù)據(jù)元素,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
Spring Boot 2結合Spring security + JWT實現(xiàn)微信小程序登錄
這篇文章主要介紹了Spring Boot 2結合Spring security + JWT實現(xiàn)微信小程序登錄,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
Java SpringBoot在RequestBody中高效的使用枚舉參數(shù)原理案例詳解
這篇文章主要介紹了Java SpringBoot在RequestBody中高效的使用枚舉參數(shù)原理案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-09-09

