用户登录功能基本测试完成

This commit is contained in:
2023-06-29 23:25:23 +08:00
parent 01d0e512df
commit 5aee01812f
8 changed files with 81 additions and 24 deletions

View File

@@ -16,9 +16,9 @@ public class UserDao {
/** /**
* 用户登录 * 用户登录
* @param userName * @param userName
* @param userPassword * @param password
* */ * */
public UserEntity UserLogin(String userName,String userPassword){ public UserEntity UserLogin(String userName,String password){
ResultSet resultSet = null; ResultSet resultSet = null;
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
Connection connection = null; Connection connection = null;
@@ -27,7 +27,7 @@ public class UserDao {
String loginSql = "select * from user where userName=? and userPassword=? and isValid=1;"; String loginSql = "select * from user where userName=? and userPassword=? and isValid=1;";
preparedStatement = connection.prepareStatement(loginSql); preparedStatement = connection.prepareStatement(loginSql);
preparedStatement.setString(1, userName); preparedStatement.setString(1, userName);
preparedStatement.setString(2, userPassword); preparedStatement.setString(2, password);
resultSet = preparedStatement.executeQuery(); resultSet = preparedStatement.executeQuery();
if (!resultSet.next()) { // 查询不到用户数据 if (!resultSet.next()) { // 查询不到用户数据
return null; return null;
@@ -36,7 +36,7 @@ public class UserDao {
Integer id = resultSet.getInt(1); Integer id = resultSet.getInt(1);
String dbUserName = resultSet.getString(2); String dbUserName = resultSet.getString(2);
String dbUserPassword = resultSet.getString(3); String dbUserPassword = resultSet.getString(3);
UserEntity userEntity = new UserEntity(dbUserName, dbUserPassword); userEntity = new UserEntity(dbUserName, dbUserPassword);
return userEntity; return userEntity;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

View File

@@ -14,6 +14,7 @@ public class UserEntity {
* `creatTime` datetime DEFAULT NULL COMMENT '创建时间', * `creatTime` datetime DEFAULT NULL COMMENT '创建时间',
* `isValid` int(11) DEFAULT NULL COMMENT '是否有效(0/1)', * `isValid` int(11) DEFAULT NULL COMMENT '是否有效(0/1)',
* `projectNum` int(11) DEFAULT NULL COMMENT '提交的项目总数', * `projectNum` int(11) DEFAULT NULL COMMENT '提交的项目总数',
* `searchHistory` varchar(255) DEFAULT NULL COMMENT '搜索历史',
* PRIMARY KEY (`userId`) * PRIMARY KEY (`userId`)
* ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COMMENT='用户表'; * ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COMMENT='用户表';
*/ */
@@ -27,6 +28,8 @@ public class UserEntity {
private Integer projectNum; private Integer projectNum;
private String searchHistory;
public UserEntity() { public UserEntity() {
} }
@@ -36,6 +39,16 @@ public class UserEntity {
this.userPassword = userPassword; this.userPassword = userPassword;
} }
public UserEntity(Integer userId, String userName, String userPassword, Date creatTime, Integer isValid, Integer projectNum, String searchHistory) {
this.userId = userId;
this.userName = userName;
this.userPassword = userPassword;
this.creatTime = creatTime;
this.isValid = isValid;
this.projectNum = projectNum;
this.searchHistory = searchHistory;
}
public Integer getUserId() { public Integer getUserId() {
return userId; return userId;
} }
@@ -84,13 +97,12 @@ public class UserEntity {
this.projectNum = projectNum; this.projectNum = projectNum;
} }
public UserEntity(Integer userId, String userName, String userPassword, Date creatTime, Integer isValid, Integer projectNum) { public String getSearchHistory() {
this.userId = userId; return searchHistory;
this.userName = userName; }
this.userPassword = userPassword;
this.creatTime = creatTime; public void setSearchHistory(String searchHistory) {
this.isValid = isValid; this.searchHistory = searchHistory;
this.projectNum = projectNum;
} }
@Override @Override
@@ -102,6 +114,7 @@ public class UserEntity {
", creatTime=" + creatTime + ", creatTime=" + creatTime +
", isValid=" + isValid + ", isValid=" + isValid +
", projectNum=" + projectNum + ", projectNum=" + projectNum +
", searchHistory='" + searchHistory + '\'' +
'}'; '}';
} }
} }

View File

@@ -30,6 +30,15 @@ public class SessionFilter implements Filter {
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse; HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
String contextPath = httpServletRequest.getContextPath(); String contextPath = httpServletRequest.getContextPath();
String origin = httpServletRequest.getHeader("Origin");
httpServletResponse.setHeader("Access-Control-Allow-Origin", origin);
httpServletResponse.setHeader("Access-Control-Allow-Methods", "*");
String headers = httpServletRequest.getHeader("Access-Control-Request-Headers");
httpServletResponse.setHeader("Access-Control-Allow-Credentials", "true");
httpServletResponse.setHeader("Access-Control-Max-Age", "3600");
// 定义一个数组 排除的请求 // 定义一个数组 排除的请求
for (int i = 0; i < excludeUrls.length; i++) { for (int i = 0; i < excludeUrls.length; i++) {
String excludeUrl = contextPath + excludeUrls[i]; String excludeUrl = contextPath + excludeUrls[i];

View File

@@ -8,7 +8,7 @@ import com.hellogithub.entity.UserEntity;
* */ * */
public class UserService { public class UserService {
private UserDao userDao=new UserDao(); private UserDao userDao=new UserDao();
public UserEntity UserLogin(String userName, String userPassword){ public UserEntity UserLogin(String userName,String password){
return userDao.UserLogin(userName,userPassword); return userDao.UserLogin(userName,password);
} }
} }

View File

@@ -1,5 +1,6 @@
package com.hellogithub.servlet; package com.hellogithub.servlet;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.hellogithub.entity.UserEntity; import com.hellogithub.entity.UserEntity;
import com.hellogithub.service.UserService; import com.hellogithub.service.UserService;
@@ -11,7 +12,7 @@ import org.apache.commons.lang3.StringUtils;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.HashMap; import java.util.*;
@WebServlet("/userLogin") @WebServlet("/userLogin")
public class UserLoginServlet extends HttpServlet { public class UserLoginServlet extends HttpServlet {
@@ -24,17 +25,29 @@ public class UserLoginServlet extends HttpServlet {
@Override @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html; charset=utf-8");
// 设置响应头允许ajax跨域访问
String curOrigin = req.getHeader("Origin");
resp.setHeader("Access-Control-Allow-Origin", curOrigin == null ? "true" : curOrigin);
resp.setHeader("Access-Control-Allow-Credentials", "true");
resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD");
resp.setHeader("Access-Control-Max-Age", "3600");
resp.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With");
resp.setContentType("application/json;charset=UTF-8");
PrintWriter writer = resp.getWriter(); PrintWriter writer = resp.getWriter();
String userName = req.getParameter("username"); String userName = req.getParameter("username");
if (StringUtils.isEmpty(userName)) { if (StringUtils.isEmpty(userName)) {
setResultError("用户名不能够是为空",writer); setResultError("用户名不能够是为空", writer);
return; return;
} }
String userPwd = req.getParameter("password"); String userPwd = req.getParameter("password");
// 参数验证 // 参数验证
if (StringUtils.isEmpty(userPwd)) { if (StringUtils.isEmpty(userPwd)) {
//转发到错误页面 //转发到错误页面
setResultError("密码不能够是为空",writer); setResultError("密码不能够是为空", writer);
return; return;
} }
String userCode = req.getParameter("code"); // 用户输入的图形验证码 String userCode = req.getParameter("code"); // 用户输入的图形验证码
@@ -42,7 +55,7 @@ public class UserLoginServlet extends HttpServlet {
HttpSession session = req.getSession(); HttpSession session = req.getSession();
String sessionCode = (String) session.getAttribute(RandomValidateCode.RANDOMVALIDATECODE); String sessionCode = (String) session.getAttribute(RandomValidateCode.RANDOMVALIDATECODE);
if (!sessionCode.equalsIgnoreCase(userCode)) { if (!sessionCode.equalsIgnoreCase(userCode)) {
setResultError("验证码不能够是为空",writer); setResultError("验证码错误!", writer);
return; return;
} }
// 在调用业务逻辑层 // 在调用业务逻辑层
@@ -54,7 +67,7 @@ public class UserLoginServlet extends HttpServlet {
} }
// 判断用户是否记住密码 // 判断用户是否记住密码
String rememberPassword = req.getParameter("remember"); String rememberPassword = req.getParameter("remember");
if ("on".equals(rememberPassword)) { if ("true".equals(rememberPassword)) {
// 如果有记住密码则 将密码保存在cookie中 // 如果有记住密码则 将密码保存在cookie中
Cookie userNameCookie = new Cookie("userName", userName); Cookie userNameCookie = new Cookie("userName", userName);
Cookie userPwdCookie = new Cookie("userPwd", userPwd); Cookie userPwdCookie = new Cookie("userPwd", userPwd);

View File

@@ -21,6 +21,16 @@ public class VerifycodeServlet extends HttpServlet {
throws ServletException, IOException { throws ServletException, IOException {
response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片 response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
response.setHeader("Pragma", "No-cache");//设置响应头信息,告诉浏览器不要缓存此内容 response.setHeader("Pragma", "No-cache");//设置响应头信息,告诉浏览器不要缓存此内容
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=utf-8");
// 设置响应头允许ajax跨域访问
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "token, Accept, Origin, X-Requested-With, Content-Type, Last-Modified");
//做浏览器兼容 //做浏览器兼容
response.setHeader("Cache-Control", "no-cache"); response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expire", 0); response.setDateHeader("Expire", 0);

View File

@@ -52,6 +52,13 @@ public class RandomValidateCode {
* @param response * @param response
*/ */
public void getRandcode(HttpServletRequest request, HttpServletResponse response) { public void getRandcode(HttpServletRequest request, HttpServletResponse response) {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=utf-8");
// 设置响应头允许ajax跨域访问
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "token, Accept, Origin, X-Requested-With, Content-Type, Last-Modified");
HttpSession session = request.getSession(); HttpSession session = request.getSession();
// BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类 // BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
@@ -68,6 +75,7 @@ public class RandomValidateCode {
for (int i = 1; i <= stringNum; i++) { for (int i = 1; i <= stringNum; i++) {
randomString = drowString(g, randomString, i); randomString = drowString(g, randomString, i);
} }
request.getSession(true);
session.removeAttribute(RANDOMVALIDATECODE); session.removeAttribute(RANDOMVALIDATECODE);
session.setAttribute(RANDOMVALIDATECODE, randomString); session.setAttribute(RANDOMVALIDATECODE, randomString);
g.dispose(); g.dispose();

4
src/config.properties Normal file
View File

@@ -0,0 +1,4 @@
driverClass=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/hellogithub?serverTimezone=GMT%2B8
user=root
password=1611