关于防止SQL注入的理解,提高系统安全性(学以致用)
这篇文章讲述的是关于SQL注入的问题,防止非法进入系统,提高系统安全性。
在开发项目的过程中,登录是几乎所有的系统都必须具备的功能,但是有的系统在开发的时候可能就没有注意到SQL注入的情况,导致用户非法登录系统。为了让用户登录确保都是系统的合法用户,那么就得防止非法用户进行SQL注入进入系统。
下面先来看一下下面这样两种登录系统的方式吧:
方式一:
登录查询数据库java代码:
String command = request.getParameter("command");if ("login".equals(command)) {String userId = request.getParameter("userId");String password = request.getParameter("password");try {User user = UserManager.getInstance().login(userId, password); //将用户信息设置到session中session.setAttribute("user_info", user); //重定向到主控页面response.sendRedirect(request.getContextPath() + "/main.jsp");}catch(UserNotFoundException e) {out.println("<script>alert('"+e.getMessage()+"')</script>");}catch(PasswordNotCorrectException e) {out.println("<script>alert('"+e.getMessage()+"')</script>");} }