springboot+security+openapi3

 一、只有openapi3时

依赖

  <dependency>
       <groupId>org.springdoc</groupId>
       <artifactId>springdoc-openapi-ui</artifactId>
  </dependency>
  <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-webflux-ui</artifactId>
  </dependency>
  <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-webmvc-core</artifactId>
  </dependency>

yml文件配置

springdoc:
  api-docs:
    path: /v3/api-docs
    enabled: true
  swagger-ui:
    path: /swagger-ui/index.html
    persistAuthorization: true
  group-configs:
    - group: 1.文件上传
      packages-to-scan: com.tly.common.minio

 二、集成security时

1.SecurityConfig继承了WebSecurityConfigurerAdapter时

重写configure方法 
@Override
    public void configure(WebSecurity web) {
        //配置跳过security验证拦截的路径,配置的放行路径
        web.ignoring().antMatchers(
                "/swagger-ui/index.html",
                "/swagger-ui.html",
                "/swagger-ui/**",
                "/v3/api-docs/**",
                "/v3/api-docs"
        );
    }

2.SecurityConfig未继承了WebSecurityConfigurerAdapter时

将跳过安全配置的方法作为bean注入  
 // 放行OpenAPI 3的路径
    @Bean
    public WebSecurityCustomizer webSecurityCustomizer() {
        return (web) -> web.ignoring().antMatchers(
                "/swagger-ui/index.html",
                "/swagger-ui.html",
                "/swagger-ui/**",
                "/v3/api-docs/**",
                "/v3/api-docs"
        );
    }

 

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot使用OpenAPI(也称为Swagger3)的步骤如下: 1. 首先,你需要导入Swagger3的依赖。在你的项目中的pom.xml文件中添加以下依赖项: ``` <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-ui</artifactId> <version>1.5.12</version> </dependency> ``` 2. 如果你的Spring Boot版本高于2.4或2.5,你需要在application.yml配置文件中增加mvc配置,以启用Swagger的接口文档生成。例如: ``` springdoc: api-docs: enabled: true ``` 3. 定义一个Swagger的配置类,用于配置Swagger的各项参数,比如文档标题、版本等。你可以通过继承`org.springdoc.core.SpringDocConfigurat`类或者使用`@Configuration`注解创建一个配置类。在配置类中,你可以使用`@Bean`注解配置Swagger的各种参数,如下所示: ``` @Configuration public class SwaggerConfig { @Bean public OpenAPI customOpenAPI() { return new OpenAPI() .info(new Info().title("API Documentation").version("1.0.0")); } } ``` 4. 现在你可以开始使用OpenAPI了。在你的Controller类或者方法上使用Swagger的注解,比如`@Operation`、`@Api`等来描述接口信息。这些注解将会被Swagger自动解析生成接口文档。 在你的浏览器中访问启动的Spring Boot应用程序的Swagger UI页面,通常是`http://localhost:8080/swagger-ui.html`,你将看到自动生成的接口文档。 以上就是Spring Boot中使用OpenAPI(Swagger3)的基本步骤。通过配置Swagger的注解和参数,你可以更详细地定义和展示你的API接口信息。如果你需要进一步了解示例代码和其他配置说明,你可以参考提供的Spring BootSpringDoc OpenAPI3整合的Demo代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值