家庭作业问题 ::我在安全编码测验中遇到了这个问题:
在Java中创建一个可接受的方法来接受字符串并将其与预定义值进行比较时,什么输入验证将使该方法具有防御性?
public static final String SLIDES="Slides";
public static final boolean isAcceptableType(String type){
if(________________________________){
return false;
}
if(!type.equals(SLIDES){
return false;
}
return true;
}
我的目标是填写if条件,以使该方法具有可辩驳性。 我无法更改其他代码。
根据我的理解,我们必须验证方法参数 以检查其有效字符串。这样在与SLIDES比较时不会抛出任何异常
我尝试过if(type == null),但结果不正确。
请帮助我解决这个问题。 :)
答案 0 :(得分:3)
不清楚为什么
if (type == null)
不行。
如果您的目标是确保type
等于SLIDES
,则可以使用:
if (!Objects.equals(type, SLIDES))
或
if (!SLIDES.equals(type))
两者都将处理type == null
的情况。
答案 1 :(得分:0)
使用空格。答案是:type == null
。如:
if(type == null){
return false;