gpt4 book ai didi

java - "You must enter a password with at least one number a lower case and a symbol"

转载 作者:行者123 更新时间:2023-12-01 16:34:55 24 4
gpt4 key购买 nike

我想加强用户注册时的密码要求。

我在servlet Registration.java中编写了以下代码:

public static boolean checkpassword(password) {
boolean hasNum = false;
boolean hasCap = false;
boolean hasLow = false;
char c;
for (int i = 0; i < password.length(); i++) {
c = password.charAt(i);
if (Character.isDigit(c)) {
hasNum = true;
} else if (Character.isUpperCase(c)) {
hasCap = true;
} else if (Character.isLowerCase(c)) {
hasLow = true;
} else if (hasNum && hasCap && hasLow) {
return true;
}
}
return false;
}

但是,我不知道将它放在下面的 servlet 中的哪里:

public class Register extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();

String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String email = request.getParameter("email");
String password = request.getParameter("password");
String password1 = request.getParameter("password1");
String country = request.getParameter("country");
String city = request.getParameter("city");
String address = request.getParameter("address");
String postalcode = request.getParameter("postalcode");

if (password == password1 && email.contains("@")) {

try {
Class.forName("org.sqlite.JDBC");
Connection con = DriverManager.getConnection("jdbc:sqlite:/Users/Oussama/login.db");
PreparedStatement ps = con.prepareStatement("insert into users values(?,?,?,?,?,?,?,?,?)");
ps.setString(2, firstname);
ps.setString(3, lastname);
ps.setString(4, email);
ps.setString(5, password);
ps.setString(6, country);
ps.setString(7, city);
ps.setString(8, address);
ps.setString(9, postalcode);

int i = ps.executeUpdate();
if (i > 0)
out.print("You are successfully registered...");

} catch (Exception e2) {
System.out.println(e2);
}

out.close();
} else {
request.setAttribute("errorMessage", "Passwords do not match");
request.getRequestDispatcher("/register.jsp").forward(request, response);
}
}
}

我尝试将其放在之前和之后,但是仍然有错误。

你们知道我把这段代码放在哪里(我相信它是有效的)吗?

提前谢谢您!

最佳答案

您检查密码强度的方法不完整。写成如下:

public static boolean isValidPassword(String password) {
String pattern = "^(?=.*?\\p{Upper})(?=.*?\\p{Lower})(?=.*?[0-9])(?=.*?\\p{Punct}).{4,}";
return password.matches(pattern);
}

说明:

  • 至少一个大写字母(?=.*?\\p{Upper})
  • 至少一个小写字母(?=.*?\\p{Lower})
  • 至少一位数字(?=.*?[0-9])
  • 至少一个特殊字符(?=.*?\\p{Punct})
  • 以上每一项均要求至少 4 个字符 {4,}

检查Pattern了解有关正则表达式模式的更多信息。

快速演示:

public class Main {
public static void main(String[] args) {
// Test
String[] testStrs = { "Hello", "Ab1#", "Abc123", "ABC123$" };
for (String s : testStrs) {
System.out.println(s + " -> " + (isValidPassword(s) ? "meets the criteria" : "does not meet the criteria"));
}
}

public static boolean isValidPassword(String password) {
String pattern = "^(?=.*?\\p{Upper})(?=.*?\\p{Lower})(?=.*?[0-9])(?=.*?\\p{Punct}).{4,}";
return password.matches(pattern);
}
}

输出:

Hello -> does not meet the criteria
Ab1# -> meets the criteria
Abc123 -> does not meet the criteria
ABC123$ -> does not meet the criteria
<小时/>

切勿使用 == 来比较字符串,它会比较引用;不是内容。您已使用过

if (password == password1)

应该是

if (password.equals(password1))
<小时/>

我相信passwordpassword1密码确认密码输入字段中的值有关。使用 isValidPassword 方法(上面提到的),如下所示:

//...
String password = request.getParameter("password");
String password1 = request.getParameter("password1");
//...

if (password.equals(password1) && isValidPassword(password) && email.contains("@")) {

//...

}

关于java - "You must enter a password with at least one number a lower case and a symbol",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61976258/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com