我有两个包含一些字符串的数组,如下所示:
数组#1
abcd
efgh
servicegroup_1
ijkl
数组#2
servicegroup_3
servicegroup_1
"VERIFY: OK"
如果 Array #1 至少包含 Array #2 的字符串之一,我想打印一个字符串(在控制台上)。否则,"VERIFY: KO"
必须打印一个字符串。
我实现了一些如下代码:
for (int i = 0; i < scopeFile.length; i++) {
for (String element : scopeJWT) {
if (scopeFile[i].contains(element)) {
ctx.setSendZuulResponse(true);
System.out.println(now + " --- " + "[Check Signature: OK]" + " [Verify Scope: OK]" + " [Verify expTime: OK]");
} else {
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
System.err.println(now + " --- " + "[VERIFY SCOPE: KO]");
}
}
}
但是,它同时返回VERIFY SCOPE: OK
and VERIFY SCOPE: KO
,即使在考虑我报告的数组时,我也会只打印VERIFY SCOPE: OK
(因为 Array #1 contains servicegroup_1
)
任何想法来修复我的代码?谢谢
在 Array1 中找到字符串后,您忘记中断循环。尝试break;
在此行后添加一个:
System.out.println(now + " --- " + "[Check Signature: OK]" + " [Verify Scope: OK]" + " [Verify expTime: OK]");
并检查循环外的非正常情况。
像这样:
boolean isOk = false;
for (int i = 0; i < scopeFile.length; i++) {
for (String element : scopeJWT) {
if (scopeFile[i].contains(element)) {
ctx.setSendZuulResponse(true);
System.out.println(now + " --- " + "[Check Signature: OK]" + " [Verify Scope: OK]" + " [Verify expTime: OK]");
isOk = true;
break;
}
}
}
if (!isOk) {
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
System.err.println(now + " --- " + "[VERIFY SCOPE: KO]");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句