spring Boot 集成swagger2全过程(代码包含集成Spring Security+ JWT)
程序员文章站
2022-05-08 14:20:30
...
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)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