springboot項(xiàng)目關(guān)閉swagger如何防止漏洞掃描
為了應(yīng)對安全掃描,再生產(chǎn)環(huán)境下關(guān)閉swagger ui
1、項(xiàng)目中關(guān)閉swagger
在這里用的是config配置文件的方式關(guān)閉的
@Configuration @EnableSwagger2 public class SwaggerConfig implements WebMvcConfigurer { @Value("${swagger.enable}") private Boolean enable; @Bean public Docket swaggerPersonApi10() { return new Docket(DocumentationType.SWAGGER_2) .enable(enable) //配置在該處生效 .select() .apis(RequestHandlerSelectors.basePackage("com.unidata.cloud.logservice.api.controller")) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .version("1.0") .title("") .contact(new Contact("", "", "")) .description("") .build(); } }
在application.properties中增加
swagger.enable: false
來控制關(guān)閉,如果想開啟就改為true
2、到這里其實(shí)已經(jīng)關(guān)閉swagger 了,但是安全掃描還是不能通過,因?yàn)樵L問swagger-ui.html路徑會跳出提示swagger已關(guān)閉的頁面,而安全掃描只要返回的頁面中含有swagger的字符,就會不通過,這里還需要一步,讓訪問swagger-ui.html頁面直接返回404
首先新增一個監(jiān)聽config
public class SwaggerInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String requestUri = request.getRequestURI(); if (requestUri.contains("swagger-ui")) { response.sendRedirect("/404"); // 可以重定向到自定義的錯誤頁面 return false; } return true; } }
然后在之前的config中添加一段代碼
@Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new SwaggerInterceptor()).addPathPatterns("/**"); }
好的,到這里就已經(jīng)徹底關(guān)閉swagger了
到此這篇關(guān)于springboot項(xiàng)目關(guān)閉swagger防止漏洞掃描的文章就介紹到這了,更多相關(guān)springboot項(xiàng)目關(guān)閉swagger內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot?AOP統(tǒng)一處理Web請求日志的示例代碼
springboot有很多方法處理日志,例如攔截器,aop切面,service中代碼記錄等,下面這篇文章主要給大家介紹了關(guān)于SpringBoot?AOP統(tǒng)一處理Web請求日志的相關(guān)資料,需要的朋友可以參考下2023-02-02基于java實(shí)現(xiàn)websocket代碼示例
這篇文章主要介紹了基于java實(shí)現(xiàn)websocket代碼示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12Java常用類庫Apache Commons工具類說明及使用實(shí)例詳解
這篇文章主要介紹了Java常用類庫Apache Commons工具類說明及使用實(shí)例詳解,需要的朋友可以參考下2020-02-02Spring Boot學(xué)習(xí)入門之統(tǒng)一異常處理詳解
我們在做Web應(yīng)用的時候,請求處理過程中發(fā)生錯誤是非常常見的情況。下面這篇文章主要給大家介紹了關(guān)于Spring Boot學(xué)習(xí)入門之統(tǒng)一異常處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-09-09Spring關(guān)于@Configuration配置處理流程
這篇文章主要介紹了Spring關(guān)于@Configuration配置處理流程,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06