【发布时间】:2014-10-12 05:21:23
【问题描述】:
全部,
我编写了一个 java 代码,需要检查用户使用控制台输入的文本,我希望代码测试输入的行不超过例如 20 个字母, 我是这样写的:
String getName() {
boolean badName = true;
String Name = "";
Scanner console = new Scanner(System.in);
while (badName ){
System.out.println("Please enter your first name ");
Name = console.nextLine();
//^ I want to check this string length while the user enters the line
// to prevent DOS attack when an attacker tries to enter very large line
if (console.nextLine().length > 20) {
//^ I tried this but could not get the string value after this condition validated,
// I dont want to store it in a variable to not cause DOS attack.
System.out.println("please enter valid name!!!");
continue;
}
if (! Name.matches ("[a-zA-Z_]+")) {
System.out.println("the name contains invalid character, please try again :");
continue;
}
if (Name.matches ("[a-zA-Z_]+")){
badName = false;
}
}
return Name;
}
不确定我是否真的需要检查以防止 DOS 攻击,或者 Java 通常会处理这个问题?
谢谢,
【问题讨论】:
-
嗯...这是什么“DOS”攻击?
-
拒绝服务攻击
-
我不认为这可以防止任何攻击,但是您正在扫描输入并将其分配给
name,因此当 if 语句尝试执行 `console.nextLine()' 时,它会发现什么都没有。 -
@Hex - 是的,DOS 就是这样,但它也是 Micro$lop 的磁盘操作系统。但是,我不确定 OP 是什么意思。如果他真的是说DOS/DDOS,那就奇怪了。
-
dramzy,这正是我得到的,当尝试检查长度而不存储值时,我丢失了值,我只能读取下一行。
标签: java string securestring secure-coding