一、配置swagger2.x
- 添加pom依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
- 启动类或者配置类上添加注解
@EnableSwagger2
@EnableSwagger2
@Configuration
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.sapphire"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("sapphire-spring-boot-demo")
.description("Sapphire demo project for Spring Boot")
.version("v1")
.build();
}
}
- spring security 配置授权认证
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
//swagger权限配置
.antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/**/api-docs").anonymous()
.anyRequest()
.authenticated();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
- 访问地址:
http://localhost:8080/swagger-ui.html
二、配置swagger3.x
注意:
swagger2.x 升级为swagger3.x,有以下几点变化:
- 注解由
@EnableSwagger2
更改为@EnableOpenApi
- 文档类型由
DocumentationType.SWAGGER_2
更改为DocumentationType.OAS_30
- 访问地址由
http://localhost:8080/swagger-ui.html
更改为http://localhost:8080/swagger-ui/index.html
- 资源路径的变更
- pom依赖由
springfox-swagger2
和springfox-swagger-ui
简化为springfox-boot-starter
。当然,旧的依赖直接升级版本号也可以
具体配置
- 添加pom依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
- 启动类或者配置类上添加注解
@EnableOpenApi
@EnableOpenApi
@Configuration
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.sapphire"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("sapphire-spring-boot-demo")
.description("Sapphire demo project for Spring Boot")
.version("v1")
.build();
}
}
- spring security 配置授权认证
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/swagger-resources/**", "/swagger-ui/**", "/**/api-docs").anonymous()
.anyRequest()
.authenticated();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
- 访问地址:
http://localhost:8080/swagger-ui/index.html