当前位置: 首页 > news >正文

swagger路径怎么设置为不曝露

为了确保 Swagger UI 不暴露在外部访问下,可以采取以下几种方法来限制访问:

方法1:通过认证和授权限制访问

为 Swagger UI 页面设置访问控制,确保只有授权用户才能访问。

1.1 Spring Security

如果使用的是 Spring Boot,可以使用 Spring Security 来保护 Swagger UI 页面。

SecurityConfig 中配置:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/webjars/**", "/swagger-ui/**").authenticated().anyRequest().permitAll().and().httpBasic(); // 可以选择其他认证方式,如 OAuth2}
}
1.2 使用 API Key 或 Token

配置 Swagger UI 使用 API Key 或 Token 进行访问。

在 Swagger 配置文件中添加 API Key 认证:

import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiKey;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;import java.util.Collections;@EnableSwagger2
public class SwaggerConfig {@Beanpublic Docket api() {return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()).build().securitySchemes(Collections.singletonList(apiKey()));}private ApiKey apiKey() {return new ApiKey("apiKey", "X-API-KEY", "header");}
}

方法2:通过配置文件进行限制

2.1 Spring Boot

application.propertiesapplication.yml 文件中配置 Swagger 的访问路径。

# application.properties
springdoc.swagger-ui.path=/hidden/swagger-ui.html

或者在 application.yml 中:

# application.yml
springdoc:swagger-ui:path: /hidden/swagger-ui.html

方法3:Nginx 或 Apache 代理服务器

在使用 Nginx 或 Apache 作为反向代理时,限制 Swagger UI 的访问。

3.1 Nginx 配置

在 Nginx 配置文件中添加访问控制:

server {listen 80;server_name yourdomain.com;location /webjars/swagger-ui/ {auth_basic "Restricted Access";auth_basic_user_file /etc/nginx/.htpasswd;proxy_pass http://localhost:8080/webjars/swagger-ui/;}
}
3.2 Apache 配置

在 Apache 配置文件中添加访问控制:

<VirtualHost *:80>ServerName yourdomain.com<Location /webjars/swagger-ui/>AuthType BasicAuthName "Restricted Access"AuthUserFile /etc/apache2/.htpasswdRequire valid-userProxyPass http://localhost:8080/webjars/swagger-ui/ProxyPassReverse http://localhost:8080/webjars/swagger-ui/</Location>
</VirtualHost>

方法4:完全禁用 Swagger UI 在生产环境

在生产环境中完全禁用 Swagger UI 以确保安全。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;@EnableSwagger2
@Configuration
public class SwaggerConfig {@Beanpublic Docket api() {return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()).build().enable(!isProduction()); // 根据环境变量或配置文件判断是否启用 Swagger}private boolean isProduction() {// 实现环境判断逻辑,例如读取环境变量或配置文件return "prod".equals(System.getProperty("spring.profiles.active"));}
}

通过这些方法,你可以有效地限制 Swagger UI 的访问,确保只有授权用户或特定环境下才能访问。选择最适合你的项目需求的方法来实现安全保护。

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • ETag:Springboot接口如何添加Tag
  • 深度分析和对比本地大语言模型Ollama和LocalAI
  • Java——继承(Inheritance)
  • 软考高级,系统架构设计师,24年5月真题,原创甄选范文!!!
  • 【网工】学习笔记1
  • leetcode刷题:vector刷题
  • 云计算【第一阶段(24)】Linux文件系统与日志分析
  • JCEF 在idea 开发 java 应用
  • 【基于R语言群体遗传学】-8-代际及时间推移对于变异的影响
  • Vue 邮箱登录界面
  • Java视频点播网站
  • LT8711GX 国产芯片 Type-C/DP1.4转HDMI2.1 用于加密狗 对接站
  • Spring:Spring框架的发展历程与学习指南
  • JVM专题之垃圾收集算法
  • SpringBoot | 大新闻项目源码打包
  • Angular2开发踩坑系列-生产环境编译
  • JAVA_NIO系列——Channel和Buffer详解
  • JavaScript 基础知识 - 入门篇(一)
  • JS学习笔记——闭包
  • python3 使用 asyncio 代替线程
  • Spring Cloud中负载均衡器概览
  • 从伪并行的 Python 多线程说起
  • 从重复到重用
  • 对超线程几个不同角度的解释
  • 和 || 运算
  • 如何学习JavaEE,项目又该如何做?
  • 一天一个设计模式之JS实现——适配器模式
  • 再谈express与koa的对比
  • 回归生活:清理微信公众号
  • 扩展资源服务器解决oauth2 性能瓶颈
  • ​​​【收录 Hello 算法】10.4 哈希优化策略
  • ​【经验分享】微机原理、指令判断、判断指令是否正确判断指令是否正确​
  • ​浅谈 Linux 中的 core dump 分析方法
  • ​业务双活的数据切换思路设计(下)
  • #etcd#安装时出错
  • (分享)自己整理的一些简单awk实用语句
  • (转)真正的中国天气api接口xml,json(求加精) ...
  • (转)总结使用Unity 3D优化游戏运行性能的经验
  • .cn根服务器被攻击之后
  • .Net 垃圾回收机制原理(二)
  • .NET/C# 如何获取当前进程的 CPU 和内存占用?如何获取全局 CPU 和内存占用?
  • .NET+WPF 桌面快速启动工具 GeekDesk
  • .NET命令行(CLI)常用命令
  • .NET序列化 serializable,反序列化
  • //解决validator验证插件多个name相同只验证第一的问题
  • /var/spool/postfix/maildrop 下有大量文件
  • @Transactional注解下,循环取序列的值,但得到的值都相同的问题
  • @开发者,一文搞懂什么是 C# 计时器!
  • @在php中起什么作用?
  • [.net]官方水晶报表的使用以演示下载
  • []AT 指令 收发短信和GPRS上网 SIM508/548
  • [240527] 谷歌 CEO 承认 AI 编造虚假信息问题难解(此文使用 @gemini 命令二次创作)| ICQ 停止运作
  • [Asp.net MVC]Asp.net MVC5系列——Razor语法
  • [BIZ] - 1.金融交易系统特点
  • [BJDCTF2020]The mystery of ip1