各位兄弟,我想问一个有关登陆验证的问题.....小弟急啊!
我的一个后台登陆页面中调用了登陆验证(login.java)类中的一个方法,这个方法返回一个boolean类型,如果返回false,刚登陆失败,但怎么样都只返回false,可是单独进行测试的时候却是正确的,各位帮我看看,谢谢:
index.jsp:部分代码:
<%
response.setHeader( "Pragma ", "No-cache ");
response.setHeader( "Cache-Control ", "no-cache ");
response.setDateHeader( "Expires ", 0);
request.setCharacterEncoding( "GBK "); //设置编码方式为gb2312
//处理表单,并进行异常处理
String Action = request.getParameter( "Action ");
if(Action!=null && Action.equals( "Login "))
{
String Page1 = (String)request.getHeader( "Referer ");//得到页面地址来源
String Page2 = request.getRequestURL().toString();//得到当前页面地址
String methon = request.getMethod();
if (methon.equals( "POST ") && Page2.equals(Page1))
{
try{
Function Fun = new Function();
//System.out.println( "成功! ");
dreamtime.dreamnews.Login login = new dreamtime.dreamnews.Login();
String IP = request.getRemoteAddr();//得到客户端Ip地址
//System.out.println(IP);
String User = request.getParameter( "User "); //得到登录用户名
//System.out.println( "用户名: " + User);
String Pwd = request.getParameter( "Pwd "); //得到登录密码
//System.out.println( "登陆密码: " + Pwd);
String sCode = Fun.CheckReplace(request.getParameter( "VerifyCode "));
if(!sCode.equals(session.getAttribute( "VerifyCode ")))
{
session.setAttribute( "error ", " <li> 验证码错误 ");
response.sendRedirect( "index.jsp ");
return;
}
if (login.LoginCheck(User,Pwd,IP))
{
//System.out.println(login.LoginCheck(User,Pwd,IP));
session.setAttribute( "Login ", "Yes ");
session.setAttribute( "AdminID ",Integer.toString(login.AdminID));
session.setAttribute( "AdminType ",Integer.toString(login.AdminType));
session.setAttribute( "AdminName ",User);
session.setAttribute( "error ", " ");
out.println( " <SCRIPT LANGUAGE= 'JavaScript '> alert( '登录成功! ');location.href= 'Admin_Main.jsp '; </SCRIPT> ");
return;
}
else
{
session.setAttribute( "error ", " <li> 用户名或密码错误 ");
out.println( " <SCRIPT LANGUAGE = 'JavaScript '> alert( '用户名或密码错误 '); </SCRIPT> ");
response.sendRedirect( "index.jsp ");
return;
}
}catch(Exception e){
response.sendRedirect( "index.jsp ");
session.setAttribute( "error ", " ");
return;
}
}
else
{
response.sendError(403, "禁止访问 ");
//out.print( "出错 " + Page1 + "; " + Page2);
return;
}
}
%>
Login.java:
package dreamtime.dreamnews;
import java.sql.*;
import dreamtime.dreamnews.DBConnection;
import dreamtime.dreamnews.Function;
import dreamtime.dreamnews.MD5;
public class Login {
DBConnection DBConn = new DBConnection();
Function Fun = new Function();
MD5 md5 = new MD5();
public static int AdminID;
public static int AdminType;
public static int LoginNum;
public Login() {
}
/***************************************************************************
* 函数名:LoginCheck
* 作 用:验证登录 参 数:s1,s2,s3: 字符串型,登录用户名,密码,IP 返回值:布尔型。登录成功返回
* True,否则返回 False
*
public boolean LoginCheck(String UserName, String UserPwd, String Ip)
throws Exception {
String[] sLog = new String[5];
//boolean OK = true;
try {
System.out.println( "用户名: " + UserName);
System.out.println( "登陆密码: " + UserPwd);
System.out.println( "用户IP: " + Ip);
Connection Conn = DBConn.getConn();
System.out.println( "数据库操作成功! ");
Statement stmt = Conn.createStatement(1004, 1007);
ResultSet rs = null;
boolean OK = true;
String AdminPwd = " ";
String User = Fun.CheckReplace(UserName);
// String User = UserName;
System.out.println( "UserName: " + User);
String Pwd = md5.getMD5ofStr(md5.getMD5ofStr(Fun
.CheckReplace(UserPwd)));
// String Pwd = md5.getMD5ofStr(UserPwd);
System.out.println( "Pwd: " + Pwd);
// System.out.println(Pwd);
String Sql = "select * from Admin where AdminName= ' " + User + " ' ";
rs = stmt.executeQuery(Sql);
sLog[0] = User;
sLog[2] = (new java.util.Date()).toLocaleString();
sLog[3] = Ip;
if (!rs.next()) {
sLog[1] = "用户登录 [ 用户名不存在 ] ";
sLog[4] = "No ";
Fun.AddLog(sLog);
OK = false;
//System.out.println( "用户名不存在! ");
} else {
AdminPwd = rs.getString( "AdminPwd ");
System.out.println(AdminPwd);
if (Pwd.equals(AdminPwd)) {
AdminID = rs.getInt( "AdminID ");
AdminType = rs.getInt( "AdminType ");
LoginNum = rs.getInt( "LoginNum ");
sLog[1] = "用户登录 ";
sLog[4] = "Yes ";
UpdateLogin(sLog[3], sLog[2], LoginNum + 1, AdminID);
Fun.AddLog(sLog);
OK = true;
//System.out.println( "数据库操作成功: " + OK);
} else {
sLog[1] = "用户登录[密码错误] ";
sLog[4] = "No ";
Fun.AddLog(sLog);
OK = false;
//System.out.println( "用户密码错误! ");
}
}
return OK;
} catch (SQLException e) {
// e.printStackTrace();
// return e.getMessage().toString();
sLog[1] = "用户登录[程序异常] ";
sLog[4] = "No ";
Fun.AddLog(sLog);
return false;
}
}
* 函数名:UpdateLogin
* 作 用:更新登录信息 参 数:s1,最后登录的IP地址 s2,最后登录的时间 iNum,登录资料 ID,管理员ID
* 返回值:布尔型。更新成功返回 Ture,否则返回 False
public boolean UpdateLogin(String s1, String s2, int iNum, int ID)
throws Exception {
String sql = " ";
try {
Connection Conn = DBConn.getConn();
Statement stmt = Conn.createStatement(1004, 1007);
// ResultSet rs = null;
sql = "update Admin set ";
sql += "LastLoginIP= ' " + s1 + " ', ";
sql += "LastLoginTime= ' " + s2 + " ', ";
sql += "LoginNum= " + iNum + " where AdminID= " + ID;
stmt.executeUpdate(sql);
stmt.close();
Conn.close();
System.out.println( "更新数据库成功! ");
return true;
} catch (SQLException e) {
// e.printStackTrace();
System.out.print(sql);
return false;
}
}
// 测试
public static void main(String[] args) throws Exception {
Login login = new Login();
// System.out.println( "AdminPwd: " + AdminPwd);
if (login.LoginCheck( "admin ", "admin ", "192.168.6.1 ")) {
System.out.println( "Success! " + login.AdminID);
} else
System.out.println( "Fail ");
}
}请问怎么回事啊,我在线等,谢谢各位!
[解决办法]
看了一下代码,目前没看出问题。
你的意思是
jsp中if (login.LoginCheck(User,Pwd,IP))就返回false,
而测试代码中输入LoginCheck则成功?
如果这样的话分别加上System.out,看看那一步不一样。
把从页面得到的输入username ,password ,ip都输出来。
再看看两边的函数走得是不是同一个方法。
[解决办法]
另外if (login.LoginCheck(User,Pwd,IP))改为
if (login.LoginCheck(User,Pwd,IP)==true)看看。
[解决办法]
如果使用的是char,在取到数据库里面的记录后记得将两边的空格去掉,
[解决办法]
对哦,注意字符串前后的空格。
使用trim修理一下。
如果直接调用函数没问题,应该数据库的处理没有问题的。
就看页面取过来的值和“admin”, "admin "是否完全一致。
[解决办法]
String Action = request.getParameter( "Action ");
===============================================
建议不要这样命名
String User = request.getParameter( "User ");===> String User =(String)request.getParameter( "User ");
String Pwd = request.getParameter( "Pwd "); ===> String Pwd =(String) request.getParameter( "Pwd ");
[解决办法]
没有反应是什么意思啊?空白页面,密码错误,用户名不存在.这么多message,都没报?
你的每个连接都正确?
[解决办法]
你现搞个最简单的用户名密码验证,其他的东西先不要搞进去,一大堆东西放在里面,眼睛都看花了
[解决办法]
问题可能发生在
......
return OK;
} catch (SQLException e) { //发生了SQLException异常
// e.printStackTrace();
// return e.getMessage().toString();
sLog[1] = "用户登录[程序异常] ";
sLog[4] = "No ";
Fun.AddLog(sLog);
return false;