java - Spring禁用Swagger-ui进行生产
问题描述
我知道如何禁用 swagger 进行生产 - 我只需要在配置类中添加注释 @Profile("!prod") :
@Configuration
@EnableSwagger2
@RequiredArgsConstructor
@Profile("!prod")
public class SwaggerConfig {
添加注释 的结果但是结果是,swagger-ui.html 在浏览器中仍然可用,只是它是空的。我想知道有没有完全禁用它的解决方案,所以页面不会加载?
解决方案
这可以通过阻止生产环境的 url 来简单地使用 spring-security 来完成。请试试 :
将依赖项(如果您使用的是 spring-boot)添加到 pom.xml :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
添加配置文件:
@Configuration
@Profile("prod")
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/**/swagger-ui.html").denyAll();
}
}
它将发送 403 禁止状态。
推荐阅读
- vb.net - 如何检查列表是否包含VB.Net中的另一个列表项
- symfony - 如何解决 CSRF 令牌无效
- c++ - 在运行时,std 库何时完全初始化以便在不破坏代码的情况下使用它?
- linux - 安装一个使用 wget 下载的 RPM 文件
- perl - 在 Perl 中使用 system() 命令时,如何将 STDERR 重定向到 STDOUT?2>&1 解决方案不起作用
- java - 在另一个类中创建一个类的对象?
- c# - 为什么 List.All(a.this != that) 优于 !List.Any(a.this = that)
- python - 如何允许 python 应用程序更改系统时间
- excel - 编写宏来组织大量信息
- javascript - 单击时切换导航按钮