SpringBoot整合Swagger頁面禁止訪問swagger-ui.html方式
SpringBoot整合Swagger頁面禁止訪問swagger-ui.html
在Spring Boot中禁止訪問Swagger UI頁面并在攔截器中進(jìn)行攔截可以通過配置Spring Security來實(shí)現(xiàn)。
下面是一個簡單的示例,演示如何實(shí)現(xiàn)這一點(diǎn):
在Spring Boot項(xiàng)目中創(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; // 放行其他請求
}
// 可以實(shí)現(xiàn) postHandle 和 afterCompletion 方法進(jìn)行相應(yīng)處理
}配置這個攔截器類并使其生效:
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和攔截器實(shí)現(xiàn)禁止訪問Swagger UI頁面 swagger-ui.html。
如果你想完全禁用Swagger UI和Swagger資源
你可以在 Spring Boot 項(xiàng)目的 application.yml 或 application.properties 文件中添加以下配置來實(shí)現(xiàn):
- 在
application.yml文件中的配置:
spring:
profiles:
swagger:
enabled: false- 在
application.properties文件中的配置:
spring.profiles.swagger.enabled=false
通過將這些配置設(shè)置為 false,你可以完全禁用 Spring Boot 中關(guān)于 Swagger UI 和 Swagger 資源的自動配置和展示。
這樣就可以確保這些端點(diǎn)和頁面對外部用戶不可見或無法訪問。
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java http連接池的實(shí)現(xiàn)方式(帶有失敗重試等高級功能)
這篇文章主要介紹了java http連接池的實(shí)現(xiàn)方式(帶有失敗重試等高級功能),具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04
springboot CompletableFuture并行計(jì)算及使用方法
CompletableFuture基于 Future 和 CompletionStage 接口,利用線程池、回調(diào)函數(shù)、異常處理、組合操作等機(jī)制,提供了強(qiáng)大而靈活的異步編程功能,這篇文章主要介紹了springboot CompletableFuture并行計(jì)算及使用方法,需要的朋友可以參考下2024-05-05
Spring?Boot中MongoTemplate從入門到實(shí)戰(zhàn)深度解析
本文介紹了Spring?Data MongoDB中的MongoTemplate,包括主要特性、核心配置、基礎(chǔ)CRUD、高級查詢與聚合操作、GridFS操作、性能優(yōu)化與監(jiān)控以及最佳實(shí)踐,通過全面的示例項(xiàng)目,展示了如何在Spring?Boot應(yīng)用中使用MongoTemplate進(jìn)行數(shù)據(jù)庫操作,感興趣的朋友跟隨小編一起看看吧2026-01-01
spring boot下mybatis配置雙數(shù)據(jù)源的實(shí)例
這篇文章主要介紹了spring boot下mybatis配置雙數(shù)據(jù)源的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
java8 stream 如何打印數(shù)據(jù)元素
這篇文章主要介紹了java8 stream 如何打印數(shù)據(jù)元素,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
Spring Boot 2結(jié)合Spring security + JWT實(shí)現(xiàn)微信小程序登錄
這篇文章主要介紹了Spring Boot 2結(jié)合Spring security + JWT實(shí)現(xiàn)微信小程序登錄,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Java SpringBoot在RequestBody中高效的使用枚舉參數(shù)原理案例詳解
這篇文章主要介紹了Java SpringBoot在RequestBody中高效的使用枚舉參數(shù)原理案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
SpringBoot事務(wù)失效的七種場景分析及解決方案
Spring Boot 中的事務(wù)失效通常是由于代理機(jī)制、異常處理、傳播行為配置不當(dāng)?shù)仍蛞鸬?通過合理配置和排查,可以有效避免事務(wù)失效問題,所以本文給大家總結(jié)了SpringBoot事務(wù)失效的七種場景分析及解決方案,需要的朋友可以參考下2025-05-05

