前提:原项目,有springsecurity,且使用jwt
1.依赖
<springdoc.version>1.6.6</springdoc.version>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>${springdoc.version}</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-security</artifactId>
<version>${springdoc.version}</version>
</dependency>
2. 配置文件
正在使用的配置:(有一些好像确实没啥用)
springdoc:
api-docs:
enabled: true
packagesToScan: com.mods.browser.controller
swagger-ui:
disable-swagger-default-url: off
csrf:
enabled: true
enabled: true
供参考的配置
springdoc:
version: '1.0.4'
packagesToScan: com.xxxx.account.controller
swagger-ui:
path: /swagger-ui.html
csrf:
enabled: true
enabled: true
display-request-duration: true
operations-sorter: method
groups-order: desc
disable-swagger-default-url: true
model-and-view-allowed: true
show-actuator: true
group-configs:
- group: account
paths-to-match: /accountTbl/**
- group: users
packages-to-scan: com.xxxx.user.controller
3. springdoc配置类
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Arrays;
@Configuration
public class Swagger3Config {
@Bean
public OpenAPI springShopOpenAPI() {
Info info = new Info()
.title("swagger3 测试-标题")
.description("这是一段描述:springboot-swagger3")
.version("v1.0.0");
SecurityScheme securityScheme = new SecurityScheme()
.type(SecurityScheme.Type.HTTP)
.scheme("bearer")
.bearerFormat("JWT")
.in(SecurityScheme.In.HEADER)
.name("Authorization");
Components components = new Components()
.addSecuritySchemes("bearer-jwt", securityScheme);
SecurityRequirement securityRequirement = new SecurityRequirement()
.addList("bearer-jwt", Arrays.asList("read", "write"));
return new OpenAPI()
.info(info)
.components(components)
.addSecurityItem(securityRequirement);
}
}
4. springsecurity配置类
- 主要是最下边的放行路径,配置类使用的其他类暂不提供,可私信
import com.mods.auth.component.JwtAuthenticationTokenFilter;
import com.mods.auth.component.RestAuthenticationEntryPoint;
import com.mods.auth.component.RestfulAccessDeniedHandler;
import com.mods.auth.costum.JwtProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private JwtProperties jwtProperties;
@Autowired
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public RestfulAccessDeniedHandler restfulAccessDeniedHandler() {
return new RestfulAccessDeniedHandler();
}
@Bean
public RestAuthenticationEntryPoint restAuthenticationEntryPoint() {
return new RestAuthenticationEntryPoint();
}
@Autowired
public JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService())
.passwordEncoder(passwordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
for (String uri : jwtProperties.getPermitAllURI()) {
http.authorizeRequests().antMatchers(uri).permitAll();
}
http
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS).permitAll()
.anyRequest().access("@rbacService.hasPermission(request,authentication)")
.and()
.cors()
.and()
.exceptionHandling()
.accessDeniedHandler(restfulAccessDeniedHandler())
.authenticationEntryPoint(restAuthenticationEntryPoint());
http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
}
@Override
public void configure(WebSecurity web) {
web.ignoring().antMatchers(
"/swagger-ui/index.html",
"/swagger-ui.html",
"/swagger-ui/**",
"/v3/api-docs/**",
"/v3/api-docs"
);
}
}
5.参考用,springfox -> springdoc
Swagger2注解 | OpenAPI3(swagger3)注解 |
---|
@ApiParam | @Parameter | @ApiOperation | @Operation | @Api | @Tag | @ApiImplicitParams | @Parameters | @ApiImplicitParam | @Parameter | @ApiIgnore | @Parameter(hidden = true) or @Operation(hidden = true) or @Hidden | @ApiModel | @Schema | @ApiModelProperty | @Schema |
|