概要
Springboot線(xiàn)上環(huán)境徹底關(guān)閉Swagger-UI
整體架構(gòu)流程
1.SwaggerConfig使用@Profile排除線(xiàn)上環(huán)境其他環(huán)境生效
2.創(chuàng)建一個(gè)控制類(lèi)使用@Profile僅線(xiàn)上環(huán)境生效,使訪(fǎng)問(wèn)swagger-ui.html返回404文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-578601.html
技術(shù)細(xì)節(jié)
/**
* @author: suitman
* @description: go fucking comment....
* @create: 2021-02-07 10:43
**/
@Configuration
@EnableSwagger2
@Profile("!prod")
public class SwaggerConfig implements WebMvcConfigurer {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
// 設(shè)置標(biāo)題
.title("****")
// 描述
.description("***")
// 作者信息
.contact(new Contact("***", null, null))
// 版本
.version("版本號(hào): 1")
.build())
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.paths(PathSelectors.any())
.build();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Profile("prod")
@RestController
@Slf4j
public class DisableSwaggerUiController {
@RequestMapping(value = "swagger-ui.html", method = RequestMethod.GET)
public void getSwagger(HttpServletResponse httpResponse) throws IOException {
httpResponse.setStatus(HttpStatus.NOT_FOUND.value());
}
}
小結(jié)
通過(guò)這種方式可以徹底關(guān)閉線(xiàn)上環(huán)境訪(fǎng)問(wèn)swagger-ui.html直接返回404文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-578601.html
到了這里,關(guān)于Springboot線(xiàn)上環(huán)境徹底關(guān)閉Swagger-UI的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!