【发布时间】:2014-06-23 23:12:27
【问题描述】:
我创建了一个登录系统,可以在密码失败超过 3 次的情况下在阻止帐户之前检查尝试次数。问题是我需要检查用户名是否存在,如果不存在,则打印一条消息,说明 id 不存在;但我不知道该怎么办。我的意思是,我有如何做的想法,但我不知道如何在 Java 上做。
这些是我存储的程序
登录
create proc usp_Login
@id varchar(50),
@pw varchar(50)
as
select id, pw from Login where id = @id and pw = @pw and lock = 0 and tries < 4
update Login set tries = 0 where id = @id and pw = @pw;
尝试
create proc usp_Attempts
@id varchar(50)
as
select id, tries from Login where id = @id and tries < 4
update Login set tries = tries+1 where tries < 4 and id = @id
屏蔽用户
create proc usp_Lock
@id varchar(50)
as
update Login set lock = 1 where id = @id
而java部分是这样的:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
CallableStatement cStmt = null;
ResultSet rs = null;
try {
cStmt = conn.prepareCall("{call usp_Login(?, ?)}");
cStmt.setString(1, this.txtID.getText());
cStmt.setString(2, this.txtPW.getText());
cStmt.execute();
rs = cStmt.getResultSet();
if (rs.next()) {
System.out.println("Login done");
cStmt.close();
rs.close();
conn.close();
} else {
cStmt = conn.prepareCall("{call usp_Attempts(?)}");
cStmt.setString(1, this.txtID.getText());
cStmt.execute();
rs = cStmt.getResultSet();
if (rs.next()) {
System.out.println("Invalid login");
} else {
cStmt = conn.prepareCall("{call usp_Lock(?)}");
cStmt.setString(1, this.txtID.getText());
cStmt.execute();
System.out.println("Login blocked");
cStmt.close();
rs.close();
conn.close();
}
}
} catch (SQLException ex) {
System.out.print(ex);
}
}
我知道这可能不是创建登录系统的最佳方式,但我仍在学习中,这是我目前获得的最佳想法。 希望有人可以帮我做身份验证部分, 提前致谢。
【问题讨论】:
-
需要审查如何“安全”存储密码。我会将身份验证与锁定过程分开;然后很容易检测到“用户名或密码无效”。如果用户名无效,要显式添加,请独立于其他条件进行检查。
-
一般来说,我从来没有在数据库中做过“尝试和阻止”部分,但是你应该检查密码的异步加密,如果没有被阻止,只需在成功时将用户尝试重置为 0 .对不起,我帮不上什么忙。
-
我没有考虑密码保护,谢谢大家的提示。
标签: java sql-server stored-procedures callable-statement