From 5aee01812fd2941032119286cc999dafebf628d2 Mon Sep 17 00:00:00 2001 From: Qing Date: Thu, 29 Jun 2023 23:25:23 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=99=BB=E5=BD=95=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=9F=BA=E6=9C=AC=E6=B5=8B=E8=AF=95=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/hellogithub/dao/UserDao.java | 8 ++--- src/com/hellogithub/entity/UserEntity.java | 31 +++++++++++++------ src/com/hellogithub/filter/SessionFilter.java | 9 ++++++ src/com/hellogithub/service/UserService.java | 4 +-- .../hellogithub/servlet/UserLoginServlet.java | 31 +++++++++++++------ .../servlet/VerifycodeServlet.java | 10 ++++++ .../hellogithub/utils/RandomValidateCode.java | 8 +++++ src/config.properties | 4 +++ 8 files changed, 81 insertions(+), 24 deletions(-) create mode 100644 src/config.properties diff --git a/src/com/hellogithub/dao/UserDao.java b/src/com/hellogithub/dao/UserDao.java index 2cffee7..d0a800b 100644 --- a/src/com/hellogithub/dao/UserDao.java +++ b/src/com/hellogithub/dao/UserDao.java @@ -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(); diff --git a/src/com/hellogithub/entity/UserEntity.java b/src/com/hellogithub/entity/UserEntity.java index c81b28a..087b55c 100644 --- a/src/com/hellogithub/entity/UserEntity.java +++ b/src/com/hellogithub/entity/UserEntity.java @@ -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 + '\'' + '}'; } } diff --git a/src/com/hellogithub/filter/SessionFilter.java b/src/com/hellogithub/filter/SessionFilter.java index c399463..8b2ef74 100644 --- a/src/com/hellogithub/filter/SessionFilter.java +++ b/src/com/hellogithub/filter/SessionFilter.java @@ -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]; diff --git a/src/com/hellogithub/service/UserService.java b/src/com/hellogithub/service/UserService.java index 96c0b30..b3c45d9 100644 --- a/src/com/hellogithub/service/UserService.java +++ b/src/com/hellogithub/service/UserService.java @@ -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); } } diff --git a/src/com/hellogithub/servlet/UserLoginServlet.java b/src/com/hellogithub/servlet/UserLoginServlet.java index 1fadae2..15d8a1c 100644 --- a/src/com/hellogithub/servlet/UserLoginServlet.java +++ b/src/com/hellogithub/servlet/UserLoginServlet.java @@ -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) { diff --git a/src/com/hellogithub/servlet/VerifycodeServlet.java b/src/com/hellogithub/servlet/VerifycodeServlet.java index a8a040b..eea51d6 100644 --- a/src/com/hellogithub/servlet/VerifycodeServlet.java +++ b/src/com/hellogithub/servlet/VerifycodeServlet.java @@ -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); diff --git a/src/com/hellogithub/utils/RandomValidateCode.java b/src/com/hellogithub/utils/RandomValidateCode.java index f115e4f..3873a1c 100644 --- a/src/com/hellogithub/utils/RandomValidateCode.java +++ b/src/com/hellogithub/utils/RandomValidateCode.java @@ -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(); diff --git a/src/config.properties b/src/config.properties new file mode 100644 index 0000000..9929a65 --- /dev/null +++ b/src/config.properties @@ -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 \ No newline at end of file