SpringBoot整合Swagger2以及生產環境的安全問題處理


https://www.cnblogs.com/i-tao/p/10548181.html

 

 

 

3.如果解決線上接口不被暴露?

 3.1 使用springboot security過濾

略……

 3.2 生產環境移除Swagger2

略……

 3.3 直接使用多環境配置,生產環境不啟用Swagger2

application.yml文件
spring:
profiles:
active: pro

application-pro.yml

復制代碼
#生產環境
server:
  port: 8080

swagger2:
  enable: false
復制代碼

2.2 Swagger2配置類增加

@Value("${swagger2.enable}")
    private boolean swagger2Enable;
復制代碼
package com.tao.springboot.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
public class Swagger2 {
    @Value("${swagger2.enable}")
    private boolean swagger2Enable;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(swagger2Enable)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.tao.springboot.action"))//controller路徑
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("標題")
                .description("描述")
                .termsOfServiceUrl("地址")
                .version("1.0")
                .build();
    }
}
復制代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM