当前位置: 首页 > article >正文

SpringDoc OpenApi学习笔记

SpringDoc OpenApi学习笔记

springboot 2.7.6+springdoc openapi1.7.0

1、依赖

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>${springdoc.version}</version>
</dependency>

2、application.yaml

默认地址是http://localhost:8080/swagger-ui/swagger-ui/index.html、http://localhost:8080/v3/api-docs,可以在application.yaml中自定义

springdoc:
  api-docs:
    enabled: true
    path: /v3/api-docs
  swagger-ui:
    path: /swagger-ui/index.html
    persist-authorization: true

3、SpringDocConfig

package cn.darkiris.doc;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SpringDocConfig {
    // swagger: http://localhost:10001/swagger-ui/swagger-ui/index.html
    // openapi: http://localhost:10001/v3/api-docs
    @Bean
    public OpenAPI customSpringDoc() {
        return new OpenAPI()
                .info(new Info()
                        .title("User API")
                        .version("1.0.0")
                        .description("This is the api for user management.")
                );
    }
}

4、与spring security整合

如果使用spring security记得放行接口文档

package cn.darkiris.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import java.security.Provider;

@Configuration
@EnableWebSecurity
@EnableMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)
public class SecurityConfig {

    @Autowired
    private CustomAuthenticationEntryPoint customAuthenticationEntryPoint;

    @Autowired
    private CustomAccessDeniedHandler customAccessDeniedHandler;

    @Autowired
    private JwtAuthenticationFilter jwtAuthenticationFilter;

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
        // 1、AuthenticationManager 委托给适当的 AuthenticationProvider(CustomAuthenticationProvider)
        // 2、AuthenticationProvider 调用 UserDetailsService(CustomUserDetailService) 的 loadUserByUsername 获取用户信息
        // 3、AuthenticationProvider 使用 PasswordEncoder(CustomPasswordEncoder) 对比 上一步获取到的用户密码
        // 4、如果验证成功,AuthenticationProvider(CustomAuthenticationProvider) 返回一个已认证的 Authentication 对象;如果失败,抛出异常
        return authenticationConfiguration.getAuthenticationManager();
    }

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .exceptionHandling()
                .authenticationEntryPoint(customAuthenticationEntryPoint)
                .accessDeniedHandler(customAccessDeniedHandler)
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                .antMatchers("/api/user/login", "/api/user/register").permitAll()
                .antMatchers("/v3/api-docs/**", "/swagger-ui/**", "/swagger-resources/**", "/webjars/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
        return http.build();
    }
}

http://www.kler.cn/a/320850.html

相关文章:

  • 魔方和群论
  • 小程序19-微信小程序的样式和组件介绍
  • hive中map_concat函数介绍
  • 基于Lora通讯加STM32空气质量检测WIFI通讯
  • 网络学习第四篇
  • 一个win32 / WTL下多线程库(CThread类)的使用心得
  • GO Ants 学习
  • C++——将数组a[5]={-1,2,9,-5,7}中小于0的元素置成0。并将其结果输出(要求:用数组名作为函数的参数来实现)
  • C++游戏
  • Linux部署python web项目Flask + gunicorn + nginx
  • 《经典图论算法》约翰逊算法(Johnson)
  • 前端插件开发用什么技术比较好,用来程序自动化下载
  • TypeScript 设计模式之【单例模式】
  • [每日一练]修复表中的名字
  • Dependency Check:一款针对应用程序依赖组件的安全检测工具
  • ACM MM24 | Hi3D: 3D生成领域再突破!新视角生成和高分辨率生成双SOTA(复旦智象等)
  • Java 编码系列:异常处理与自定义异常
  • 一些广泛认可的编程工具,在不同的方面帮助我们提升效率
  • 使用cmd命令窗口操作mongodb
  • Scikit-LearnTensorFlow机器学习实用指南(三):一个完整的机器学习项目【下】
  • mask2former训练自定义数据集
  • Leetcode算法基础篇-位运算
  • 架构师论文备考-论软件系统架构评估
  • 云轴科技ZStack AIOS平台智塔亮相华为全联接大会
  • 在 macOS 上安装 ADB给安卓手机装APK,同样适用智能电视、车机
  • 单词的秘密2