用户登录SQL漏洞

补充:数据库自复制:insert into users(username,password,email,grade) select username,password,email,grade from users.

1 SQL漏洞,本人在SQL Sever2008下亲测

因此一般情况下,用户输入任意用户名,密码输为:XXXX'or 1='1就可以验证通过。

一般的代码验证方法:

//或者这样查询:
            rs=st.executeQuery("select top 1 * from aaaa where Name='"+u+"'and Password='"+p+"'");//加top 1能提高效率,
                                                                                               //找到一个就返回,不然会查找万整个表才结束
            if(rs.next()) {
                HttpSession hs = request.getSession(true);//为每一个用户建立一个session,通过wel.jsp的检测,防止非法登录。
                hs.setMaxInactiveInterval(20);
                hs.setAttribute("name", u);
                response.sendRedirect("wel.jsp?name=" +u+"&psw="+p); //这样有漏洞。
            }*/

所以要通过制定用户名取得密码,然后对比密码是否正确来验证

//获取用户信息
        String u = request.getParameter("usename");
        String p = request.getParameter("passwd");

        //到数据库中验证用户
        String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";//加载JDBC驱动" 
        String dbURL = "jdbc:sqlserver://localhost:50573; DatabaseName=JspDB"; //JDBC协议 
        String userName = "jsptest"; //用户名 (确保该用户和数据库有映射关系,即有权限访问)    
        String userPwd = "ygl900219"; //密码     
        Connection dbConn = null;
        try {
            Class.forName(driverName);
            dbConn = DriverManager.getConnection(dbURL, userName, userPwd); //连接服务器和数据库JspDB     
            System.out.println("Connection Successful!"); //如果连接成功 控制台输出Connection Successful!      
        } catch (Exception e) {
            e.printStackTrace();
        }
        //建立Statement对象

        Statement st = null;//Statement 提供执行基本SQL语句操作的功能
        ResultSet rs = null;
        try {
            st = dbConn.createStatement();
            rs = st.executeQuery("select top 1 Password from aaaa where Name='"+ u + "'");
            
            //获得查询结果,rs.next()成立说明用户存在。
            if (rs.next()) {

                //说明用户名存在
                if (rs.getString("Password").equals(p)) {
                    //一定是合法的
                    HttpSession hs = request.getSession(true);//为每一个用户建立一个session,通过wel.jsp的检测,防止非法登录。
                    hs.setMaxInactiveInterval(20);
                    hs.setAttribute("name", u);
                    response.sendRedirect("wel.jsp?name=" +u+"&psw="+p);
                    
                
                } else {
                    //密码错误
                    response.sendRedirect("login.jsp");
                }
            } 
            else {
                //用户名都不存在
                response.sendRedirect("login.jsp");
            }
        } 
        catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) { //注意关闭顺序,后建立的先关闭.关闭前先判断是否为空,增强代码健壮性
                    rs.close();
                }
                if (st != null) {
                    st.close();
                }
                if (dbConn != null) {
                    dbConn.close();
                }
            } catch (Exception ex) {

            }

        }


wel.jap页面利用session防止非法登录级直接登录网址http://localhost:8080/myJsp3/wel.jsp

<%
HttpSession hs=request.getSession(true);
String val=(String)hs.getAttribute("name");
if(val==null) {
    try {
        //非法登陆
        response.sendRedirect("login.jsp");
    }
    catch(Exception ex) {
        ex.printStackTrace();
    }
}
%>

<%
String u=request.getParameter("name");
String p=request.getParameter("psw");
%>
恭喜登陆!Hello <%=u+" psw="+p %><br/>
<a href="login.jsp">返回重新登录</a>

注意:以上代码引用了HttpService类,所以在项目的/WEB-INF/lib下放了tomcat的lib库下的servlet-api.jar

原文地址:https://www.cnblogs.com/Yogurshine/p/2990316.html