Spring Boot整合Swagger3、Knife4j

本文档介绍了如何在SpringBoot项目中配置Swagger3和Knife4j,用于生成在线接口文档。详细步骤包括添加相关依赖、配置Swagger的Docket bean以及SpringSecurity的安全框架放行路径设置。此外,还提到了高版本SpringBoot可能存在的兼容性问题。
摘要由CSDN通过智能技术生成

Swagger在配置类中写法

启动项目直接访问 ip:端口号/路径

比如:

Swagge在线接口文档访问路径: ip:端口号/swagger-ui/index.html

Knife4j在线接口文档访问路径: ip:端口号/doc.html

springboot版本太高的话可能会导致不兼容(2.6以上)

依赖

<!--swagger2 依赖-->
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger-ui</artifactId>
	<version>3.0.0</version>
</dependency>
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger2</artifactId>
	<version>3.0.0</version>
	<scope>compile</scope>
</dependency>
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-boot-starter</artifactId>
	<version>3.0.0</version>
</dependency>
<!--整合Knife4j-->
<dependency>
	<groupId>com.github.xiaoymin</groupId>
	<artifactId>knife4j-spring-boot-starter</artifactId>
	<version>3.0.3</version>
</dependency>
<!--如果SpringBoot版本大于2.3则需要引入下面依赖-->
<dependency>
	<groupId>javax.validation</groupId>
	<artifactId>validation-api</artifactId>
	<version>2.0.1.Final</version>
</dependency>

配置类

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration //配置类
//@EnableSwagger2 //swagger注解
@EnableOpenApi //开启swagger注解
@EnableKnife4j
public class SwaggerConfig {
    @Bean
    public Docket createDocket(){
        //添加摘要信息
        ApiInfoBuilder apiInfoBuilder=new ApiInfoBuilder();
        ApiInfo apiInfo = apiInfoBuilder
                //设置标题
                .title("在线接口文档")
                //描述
                .description("API在线接口文档管理")
                //版本
                .version("1.0.0")
                //作者信息
                .contact(new Contact("admin", null, null))
                .build();

        return new Docket(DocumentationType.OAS_30)
                //是否启用Swagger
                .enable(true)
                //用来创建该API的基本信息,展示在文档的页面中(自定义展示的信息)
                .apiInfo(apiInfo)
                //设置哪些接口暴露给Swagger展示
                .select()
                // 扫描所有
                .apis(RequestHandlerSelectors.any())
                //扫描指定包中的swagger注解
                //.apis(RequestHandlerSelectors.basePackage("com.csdn.controller"))
                //扫描所有有注解的api,用这种方式更灵活
                //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class));
                .paths(PathSelectors.any()).build();
    }
}

Spring Security 安全框架需要放行路径

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            //关闭csrf
            .csrf().disable()
            //不通过Session获取SecurityContext
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            .authorizeRequests()
            // 对于注册和登录接口 允许匿名访问
            .antMatchers("/login","/register").anonymous()
            //放行swagger3、Knife4j
//                .antMatchers("/swagger-ui.html","/swagger-ui/index.html","/swagger-resources/**","/swagger-ui/**","/webjars/**","/v3/**","/api/**").permitAll()
            .antMatchers("/swagger-ui/**", "/swagger-resources/**", "/v3/**",
                    "/doc.html","/webjars/**","/favicon.ico", "/img.icons/**").permitAll()
            // 除上面外的所有请求全部需要鉴权认证
            .anyRequest().authenticated();

    //把token校验过滤器添加到过滤器链中
    http.addFilterBefore(tokenFilter, UsernamePasswordAuthenticationFilter.class);

    //配置异常处理器
    http.exceptionHandling()
            //配置登陆失败处理器
            .authenticationEntryPoint(authenticationEntryPoint);

    //允许跨域
    http.cors();
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值