774 Star 6.7K Fork 1.4K

GVP萧明/knife4j

 / 详情

开启生产环境,屏蔽所有资源接口 knife4j.production=true 没起作用

已完成
创建于  
2022-12-26 18:15

pom.xml 文件

<dependency>
  <groupId>com.github.xiaoymin</groupId>
  <artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
  <version>4.0.0</version>
</dependency>

application.yml

server:
  port: 9001
  profiles:
    active: prod

application-prod.yml

knife4j:
 production: true

目的是开启生产环境,屏蔽所有资源接口,但是访问http://ip:port/doc.html依旧可以访问到接口,清空缓存也一样。
这个配置似乎没起到关闭的作用。

评论 (12)

行健 创建了任务
行健 修改了描述
行健 修改了描述
展开全部操作日志

你不把knife4j自己的开关(knife4j.enable=true)打开,他怎么生效啊

spring:
  profiles.active: prod
knife4j:
  production: true # 开启屏蔽文档资源
  enable:  true 

enable:false可以正常启动,enable:true的时候报错:
ailed to instantiate [com.github.xiaoymin.knife4j.spring.filter.ProductionSecurityFilter]: Factory method 'productionSecurityFilter' threw exception; nested exception is java.lang.NullPointerException

行健 修改了描述
<dependency>
  <groupId>com.github.xiaoymin</groupId>
  <artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
  <version>4.0.0</version>
</dependency>

根据作者的建议将 knife4j 的 enable: true 打开。
application.yml

spring:
  profiles.active: prod
knife4j:
  production: true # 开启屏蔽文档资源
  enable:  true 

enable:false可以正常启动,enable:true的时候报错,这个有哪位同学遇到过?
ailed to instantiate [com.github.xiaoymin.knife4j.spring.filter.ProductionSecurityFilter]: Factory method 'productionSecurityFilter' threw exception; nested exception is java.lang.NullPointerException

NPE异常在哪行?

输入图片说明

根据报错我跟踪了下源代码:发现以上图片信息。然后尝试knife4j.setting.custom-code=200就可以了,但我补知道为什么?因为上面图片中还有下面这个方法啊。为什么一定要加上

knife4j.setting.custom-code=200 #为什么一定要加上这句
knife4j.production=true
public ProductionSecurityFilter(boolean production) {
        this.production = production;
    }

那我大概清楚了

请问作者,是什么问题啊?

另外,在作者提供的 swagger-bootstrap-ui-gateway 这个 网关 工程中:

application.properties 增加如下,但并没有屏蔽掉接口文档。如果是单独的spring boot工程是可以生效的。

#屏蔽接口文档
knife4j.enable=true
knife4j.setting.custom-code=200
knife4j.production=true

Knife4j提供的是基于Servlet Filter体系的,Spring Cloud Gateway网关不是基于Servlet技术栈

那么在spring cloud gateway网关下有什么办法屏蔽掉文档接口吗。当然如果我注释或者删除掉config包下的配置文件是可以的。但可以从application这种配置文件下通过设置配置进行吗?还是目前暂不支持。

网关屏蔽 无需knife4j来提供,这个自己业务系统,写个Gateway下的filter不就搞定了

明白了,谢谢。

行健 任务状态待办的 修改为已完成

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(2)
118100 xiaoym 1578918321
Java
1
https://gitee.com/xiaoym/knife4j.git
git@gitee.com:xiaoym/knife4j.git
xiaoym
knife4j
knife4j

搜索帮助

Cb406eda 1850385 E526c682 1850385