gpt4 book ai didi

java - 让JSP登录表单更安全

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:40:34 24 4
gpt4 key购买 nike

我正在使用 Eclipse 创建我的第一个网站,尽管我确实还没有发现任何需要 IDE 的地方。它创建了登录表单,我认为由于使用 PreparedStatement ,它不会被注入(inject)。我想知道是否有任何其他方法可以通过简单的步骤使其安全,以及我现在拥有的算法有哪些缺点。所以,这是代码:

登录.jsp

<form action="log.jsp">
<input type="email" placeholder="email" name="userMail"/>
<input type="password" placeholder="Password" name="userPassword" />
<span>
<input type="checkbox" class="checkbox">
Keep me signed in
</span>
<button type="submit" class="btn btn-default">Login</button>
</form>

log.jsp

<%@ page import ="java.sql.*" %>
<%
String userMail = request.getParameter("userMail");
String userPassword = request.getParameter("userPassword");
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/book_store", "root", "daters");
String sql = "SELECT * FROM users WHERE userMail=? AND userPassword=?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, userMail);
ps.setString(2, userPassword);
System.out.println(sql);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
session.setAttribute("userMail", userMail);
response.sendRedirect("index.jsp");
} else {
out.println("Invalid password <a href='index.jsp'>try again</a>");
}
%>

最佳答案

既然您说这是您的第一次尝试,并且想必您想要变得更好 - 这里有几点需要注意。

a) 设计模式——MVC JSP 是 MVC 的 V 部分 - 您的程序是 MV 和 C 都集成到一个大 V 中。

b) 一旦开始创建模型,您可以在那里应用额外的验证 - 空检查等。

c) 你最终需要坚持用户登录的事实,并在其他请求中使用该信息..

关于java - 让JSP登录表单更安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34232092/

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