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

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

View File

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

View File

@@ -30,6 +30,15 @@ public class SessionFilter implements Filter {
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
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++) {
String excludeUrl = contextPath + excludeUrls[i];

View File

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

View File

@@ -1,5 +1,6 @@
package com.hellogithub.servlet;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hellogithub.entity.UserEntity;
import com.hellogithub.service.UserService;
@@ -11,7 +12,7 @@ import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.*;
@WebServlet("/userLogin")
public class UserLoginServlet extends HttpServlet {
@@ -19,22 +20,34 @@ public class UserLoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer =resp.getWriter();
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();
String userName = req.getParameter("username");
if (StringUtils.isEmpty(userName)) {
setResultError("用户名不能够是为空",writer);
setResultError("用户名不能够是为空", writer);
return;
}
String userPwd = req.getParameter("password");
// 参数验证
if (StringUtils.isEmpty(userPwd)) {
//转发到错误页面
setResultError("密码不能够是为空",writer);
setResultError("密码不能够是为空", writer);
return;
}
String userCode = req.getParameter("code"); // 用户输入的图形验证码
@@ -42,19 +55,19 @@ public class UserLoginServlet extends HttpServlet {
HttpSession session = req.getSession();
String sessionCode = (String) session.getAttribute(RandomValidateCode.RANDOMVALIDATECODE);
if (!sessionCode.equalsIgnoreCase(userCode)) {
setResultError("验证码不能够是为空",writer);
setResultError("验证码错误!", writer);
return;
}
// 在调用业务逻辑层
UserEntity adminUserEntity = userService.UserLogin(userName, userPwd);
if (adminUserEntity == null) {
// 用户名称或者密码错误!
setResultError("用户名或者密码错误!!!",writer);
setResultError("用户名或者密码错误!!!", writer);
return;
}
// 判断用户是否记住密码
String rememberPassword = req.getParameter("remember");
if ("on".equals(rememberPassword)) {
if ("true".equals(rememberPassword)) {
// 如果有记住密码则 将密码保存在cookie中
Cookie userNameCookie = new Cookie("userName", userName);
Cookie userPwdCookie = new Cookie("userPwd", userPwd);
@@ -64,7 +77,7 @@ public class UserLoginServlet extends HttpServlet {
// 能够db中查询到对象 登录成功了 将用户数据存放在session中
session = req.getSession();
session.setAttribute("user", adminUserEntity);
setResultOK("登录成功!",writer);
setResultOK("登录成功!", writer);
}
public void setResult(Integer code, String msg, PrintWriter writer) {

View File

@@ -21,6 +21,16 @@ public class VerifycodeServlet extends HttpServlet {
throws ServletException, IOException {
response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
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.setDateHeader("Expire", 0);

View File

@@ -52,6 +52,13 @@ public class RandomValidateCode {
* @param 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();
// BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
@@ -68,6 +75,7 @@ public class RandomValidateCode {
for (int i = 1; i <= stringNum; i++) {
randomString = drowString(g, randomString, i);
}
request.getSession(true);
session.removeAttribute(RANDOMVALIDATECODE);
session.setAttribute(RANDOMVALIDATECODE, randomString);
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