swagger添加访问密码使用:swagger-bootstrap-ui,@EnableSwaggerBootstrapUI

swagger添加访问密码

  • https://blog.csdn.net/qq_36090537/article/details/127789962

swagger现在是很普遍使用的接口文档。

但当项目发布到正式环境之后,swagger暴露给外部是很致命的,因此可以使用添加用户密码访问

(也可以设置swagger隐藏,利用@Profile对不同环境做不同操作,选择展示或者隐藏)

先展示实现效果

img

pom

   		<!-- swagger2-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger.version}</version>
        </dependency>
 
        <!--swaggerUI框架-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.4</version>
        </dependency>

application.yml

展示application.yml文件内需要添加的内容

切记swagger.production 不可设置为true,否则将屏蔽所有资源

swagger:
  production: false
  basic:
    enable: true
    username: root
    password: test

配置文件

swagger配置文件

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
/**
 * @author wsj
 * @Date 2019/8/20
 */
@EnableSwaggerBootstrapUI//(该注解swagger需要配置登录用户和密码才需要)
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
 
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo()) //apiInfo
//                .enable(isEnable)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.test.api"))
                .paths(PathSelectors.any())
                .build();
    }
 
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("后台管理系统API")
                .termsOfServiceUrl("http://localhost:8899/")
                .version("1.0")
                .build();
    }
}
@EnableSwaggerBootstrapUI该注解正常使用swagger无需添加,需要用到登录访问时再添加。

以上就完成了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值