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 {@Beanpublic 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();
}

Spring Boot整合Swagger3、Knife4j相关推荐

  1. Spring Boot整合Swagger3配置全局Token

    应用背景:Swagger配置全局Token的目的在于调用真正接口前会被相关拦截器拦截,拦截器会校验此次访问是否合法,这时配置全局Token的作用就显现出来了,全局Token可以存储所有接口访问时的令牌 ...

  2. Spring Boot整合Swagger3注解@ApiImplicitParam的paramType属性为“path“

    Spring Boot整合Swagger3的依赖版本为: <!--引入SpringBoot整合Swagger3的依赖--> <dependency><groupId> ...

  3. Spring Boot整合Swagger3注解@ApiImplicitParam的allowMultiple属性

    Spring Boot整合Swagger3的依赖版本为: <!--引入SpringBoot整合Swagger3的依赖--> <dependency><groupId> ...

  4. Spring Boot整合Swagger3

    第一步:引入SpringBoot整合Swagger2的最版版本3.0.0,目前的更新时间为2020年7月 <!--引入SpringBoot整合Swagger3的依赖--> <depe ...

  5. Spring Boot整合Swagger3的分组问题

    Swagger3如果没有设置分组,则所有的API接口全在一个default分组中,如下所示: 但是如果功能模块和接口数量逐渐增多时,就会显得有些凌乱,不方便查找和使用,这时可提供的解决方法就是对API ...

  6. spring boot整合spring security笔记

    最近自己做了一个小项目,正在进行springboot和spring Security的整合,有一丢丢的感悟,在这里分享一下: 首先,spring boot整合spring security最好是使用T ...

  7. RabbitMQ使用及与spring boot整合

    1.MQ 消息队列(Message Queue,简称MQ)--应用程序和应用程序之间的通信方法 应用:不同进程Process/线程Thread之间通信 比较流行的中间件: ActiveMQ Rabbi ...

  8. Spring Boot 教程(三): Spring Boot 整合Mybatis

    教程简介 本项目内容为Spring Boot教程样例.目的是通过学习本系列教程,读者可以从0到1掌握spring boot的知识,并且可以运用到项目中.如您觉得该项目对您有用,欢迎点击收藏和点赞按钮, ...

  9. 五、spring boot整合mybatis-plus

    spring boot整合mybatis-plus 简介 mybatis 增强工具包,简化 CRUD 操作. 文档 http://mp.baomidou.com http://mybatis.plus ...

最新文章

  1. Flink从入门到精通100篇(二十三)-Apache Flink在滴滴的应用与实践
  2. [答疑]-中断流程举例:在TEE侧时产生了FIQ,回到REE后为啥又产生了IRQ
  3. java换行符分隔字符串_Java 实例 – 字符串分隔(StringTokenizer) | 菜鸟教程
  4. JavaScript-简单的页面输入控制
  5. 11_HTML5_Local_Storage本地存储
  6. LeetCode 45 跳跃游戏||
  7. 坚果pro2 android 8,手机 篇一:坚果Pro2特别版使用感受
  8. 弹性力学方程 有限差分法matlab,泊松方程的有限差分法的MATLAB实现
  9. unity打开内嵌游戏网页
  10. 迈普路由器访问控制列表配置命令_迈普路由器配置命令集合
  11. python 复选框_每日一练:Python复选框的运用
  12. Windows系统下GIT生成密钥和添加密钥git
  13. 使用命令结束Linux系统
  14. ESP8266 WIFI kill 2021版教程(小白0基础)
  15. 网页直播源码IM即时通讯协议
  16. [总结]Android系统体系结构
  17. 【word使用】word文档查看字符统计
  18. 华为设备DHCP配置命令
  19. idea intellij 教育版申请
  20. CSDN的博客搜索功能太弱,教你怎么搜索自己博客的文章

热门文章

  1. Linux下套接字详解(二)----套接字Socket
  2. Django 中 a href标签 使用方法 跳转页面(Django第四篇)
  3. 一个屌丝程序员的青春(二三三)
  4. 社群本质不是转化丨社群运营核心工作更不是销售
  5. CAD怎么标注尺寸?这样操作观赏性倍增!
  6. 男宝宝和女宝宝教育不同
  7. Labrika评测:AI工具做网站页面SEO内容优化
  8. oppor11可以按Android吗,OPPO R11有NFC吗?OPPOR11支持NFC功能吗?
  9. 通过sql语句发送微信消息(转)
  10. 梦幻手游服务器总维护,《梦幻西游》手游4月22日维护更新内容解读