我正在使用Swagger和Spring MVC。我想在特定环境(如生产)中有选择地禁用招摇。我怎样才能做到这一点?
答案 0 :(得分:37)
配置swagger spring-mvc插件时,您可以使用基于环境/配置文件等传递布尔值的enable
方法。
@Bean
public SwaggerSpringMvcPlugin customImplementation(){
return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
.apiInfo(apiInfo())
.enable(environmentSpeficicBooleanFlag) //<--- Flag to enable or disable possibly loaded using a property file
.includePatterns(".*pet.*");
}
另一种方法是使用弹簧轮廓
@Bean
@Profile("production")
public SwaggerSpringMvcPlugin customImplementation(){
return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
.apiInfo(apiInfo())
.enable(false) //<--- Flag set to false in the production profile
.includePatterns(".*pet.*");
}
配置swagger spring-mvc插件时,您可以使用基于环境/配置文件等传递布尔值的enable
方法。
@Bean
public Docket customImplementation(){
return new Docket(SWAGGER_2)
.apiInfo(apiInfo())
.enable(environmentSpeficicBooleanFlag) //<--- Flag to enable or disable possibly loaded using a property file
.includePatterns(".*pet.*");
}
另一种方法是使用弹簧轮廓
@Bean
@Profile("production")
public Docket customImplementation(){
return new Docket(SWAGGER_2)
.apiInfo(apiInfo())
.enable(false) //<--- Flag set to false in the production profile
.includePatterns(".*pet.*");
}
答案 1 :(得分:3)
Dilip的回答是你要求的(我还没有测试过)。但我有一个额外的方案来处理可能感兴趣:在公共测试盒上,我希望文档启用但是私有。
我已将以下内容添加到我的WebMvcConfigurerAdapter中,它添加了基本身份验证
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new SwaggerInterceptor())
.addPathPatterns("/api-docs");
}
private class SwaggerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (!authHeaderValid(request.getHeader("Authorization"))) {
response.addHeader("Access-Control-Allow-Origin", "null");
response.addHeader("WWW-Authenticate", "Basic realm=\"\"");
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.getWriter().println("HTTP Status " + HttpServletResponse.SC_UNAUTHORIZED);
return false;
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { }
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { }
private boolean authHeaderValid(String authorization) {
if (authorization != null && authorization.startsWith("Basic ")) {
final String[] values = new String(Base64.getDecoder().decode(authorization.substring("Basic ".length()))).split(":");
return values[0].equals("username") && values[1].equals("password");
}
return false;
}
}
答案 2 :(得分:-2)
你试试这种方式
@Configuration
@EnableSwagger
// Loads the spring beans required by the framework
public class MySwaggerConfig
{
private SpringSwaggerConfig springSwaggerConfig;
/**
* Required to autowire SpringSwaggerConfig
*/
@Autowired
public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig)
{
this.springSwaggerConfig = springSwaggerConfig;
}
/**
* Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc
* framework - allowing for multiple swagger groups i.e. same code base
* multiple swagger resource listings.
*/
@Bean
public SwaggerSpringMvcPlugin customImplementation()
{
return new SwaggerSpringMvcPlugin(this.springSwaggerConfig).apiInfo(apiInfo()).includePatterns(
".*?");
}
private ApiInfo apiInfo()
{
ApiInfo apiInfo = new ApiInfo(
"xx",
"xxxx",
"My Apps API terms of service",
"xxx",
null,
null);
return apiInfo;
}
}
pom是swagger-springmvc。 veriosn是0.9.5,
之后启动服务器