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 {

    @Override
    protected 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 {

    @Bean
    public 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 Basic
        AuthName "Restricted Access"
        AuthUserFile /etc/apache2/.htpasswd
        Require valid-user
        ProxyPass 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 {

    @Bean
    public 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 的访问,确保只有授权用户或特定环境下才能访问。选择最适合你的项目需求的方法来实现安全保护。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MonkeyKing.sun

对你有帮助的话,可以打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值