springSecurity整合knief4j接口文档 knife4j-spring-boot-starter

springSecurity整合knief4j接口文档

  1. 添加如下依赖
        <!--knife4j(接口文档)-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
            <version>4.4.0</version>
        </dependency>
        <!--spring-boot版本不要太高-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>
  1. 在yml配置文件中添加以下内容:主要是在spring底下mvc
spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
  1. 在springsecurity中放行
@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                //关闭跨域校验
                .csrf().disable()
                //关闭security默认session验证
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                //授权
                .authorizeRequests()
                //放行想要放行的资源
                .antMatchers(
                       //下面是knief4j和swagger放行的内容,包含了好几个版本的knief4j,所以直接全部复制就行了
                        ,"/v3/api-docs"
                        , "/api/**"
                        ,"/doc.html"
                        , "/webjars/**"
                        , "/img.icons/**"
                        , "/swagger-resources/**"
                        , "/**"
                        , "/v2/api-docs"
                ).permitAll() // 允许匿名访问
                // 任何请求都需要认证
                .anyRequest().authenticated();

    }
  1. 在yml文件中配置你的controller的位置
# 接口文档配置
knife4j:
  enable: true
  openapi:
    title: "接口文档"
    version: 1.0
    group:
      default:
        api-rule: package
        api-rule-resources:
          - com.test.controller # 你的接口的位置
  1. 访问路径即可:http://localhost:{port}/doc.html port是你的端口号 例如下方
http://localhost:8080/doc.html#/home
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值