背景

最近公司出现一个事故,我们都知道做资讯的很重要的就是敏感词过滤,一些敏感词不过滤可能是要进去踩缝纫机的。而这次的事故就是,公司的某个团队的敏感词过滤是通过开关进行配置,具体是在nacos还是apollo上不清楚。而因为命名的问题,导致敏感词过滤功能没开起来,最后被人截图勒索,当然公司最终选择了报警。

事故过程

模拟下事故过程。有以下几个变量

private String isPass;
private Boolean isPass1;
private boolean isPass2;

同时在nacos进行配置:

isPass: true
isPass1: true
isPass2: true

如果程序启动后,上面几个变量接收到的结果是什么

isPass="true"
isPass1=true
isPass2=false

为什么呢?

关于 Java Bean 中的 getter/setter 方法的定义其实是有明确的规定的,根据 JavaBeans (TM) Specification 规定,如果是普通的参数,命名为 propertyName,需要通过以下方式定义其 setter/getter:

    public <PropertyType> get<PropertyName>();
    public void set<PropertyName>(<PropertyType> p)

但是,布尔类型的变量 propertyName 则是另外一套命名原则的:

    public boolean is<PropertyName>();
    public void set<PropertyName>(boolean p)

所以,boolean类型实际的字段是pass2,而不是isPass2。

而boolean在类变量中默认值是false。所以造成的结果就是isPass2的配置不生效。造成了这次事故。