spring Boot 集成swagger2全过程(代码包含集成Spring Security+ JWT)

1:Spring Boot直接集成
1)maven添加Swagger2的依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
2)application.yml配置
spring:
resources:
add-mappings: true

springfox:
documentation:
swagger:
v2:
path: /api-docs

3)添加Swagger2配置类

4)controller方法上进行swagger注解

5)这样最基本的Spring Boot集成swagger就完成了,访问host:port/swagger-ui.html就可以访问了

2:集成spring security和jwt
1)配置SecurityConfiguration的web.ignoring().antMatchers("/api-docs", "/swagger-resources/**", "/swagger-ui.html**", "/webjars/**")

2)修改Swagger2配置,加上
.securitySchemes(newArrayList(apiKey()))
.securityContexts(newArrayList(securityContext()))

3)做完上面两步就可以添加token认证了

3:配合Spring Cloud Zuul进行集中式整合Swagger文档到同一个页面上,除了网关之外的微服务只需要实现上面两步就可以了(第2步中除网关外的微服务不需要配置SecurityConfiguration,只需要修改Swagger2配置)

1)添加DocumentationConfig类继承SwaggerResourcesProvider

2)配置SecurityConfiguration的web.ignoring().antMatchers("/api-docs", "/swagger-resources/**", "/swagger-ui.html**", "/webjars/**","/medical-provider/api-docs")

3)这样就可以就行集中管理了

4:通过Eureka的链接跳转到Swagger-ui页面

1)在application.yml加上如下配置即可
eureka:
instance:
status-page-url: http://localhost:${server.port}/swagger-ui.html
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值