diff --git a/src/com/hellogithub/dao/AdminDao.java b/src/com/hellogithub/dao/AdminDao.java new file mode 100644 index 0000000..fb313b2 --- /dev/null +++ b/src/com/hellogithub/dao/AdminDao.java @@ -0,0 +1,4 @@ +package com.hellogithub.dao; + +public class AdminDao { +} diff --git a/src/com/hellogithub/dao/UserDao.java b/src/com/hellogithub/dao/UserDao.java new file mode 100644 index 0000000..2cffee7 --- /dev/null +++ b/src/com/hellogithub/dao/UserDao.java @@ -0,0 +1,48 @@ +package com.hellogithub.dao; + +import com.hellogithub.entity.UserEntity; +import com.hellogithub.utils.JdbcUtils; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; + +/** + * 用户Dao层 + * */ +public class UserDao { + private UserEntity userEntity=new UserEntity(); + + /** + * 用户登录 + * @param userName + * @param userPassword + * */ + public UserEntity UserLogin(String userName,String userPassword){ + ResultSet resultSet = null; + PreparedStatement preparedStatement = null; + Connection connection = null; + try { + connection = JdbcUtils.getConnection(); + String loginSql = "select * from user where userName=? and userPassword=? and isValid=1;"; + preparedStatement = connection.prepareStatement(loginSql); + preparedStatement.setString(1, userName); + preparedStatement.setString(2, userPassword); + resultSet = preparedStatement.executeQuery(); + if (!resultSet.next()) { // 查询不到用户数据 + return null; + } + // 将db中数据 返回给客户端 查询到数据 + Integer id = resultSet.getInt(1); + String dbUserName = resultSet.getString(2); + String dbUserPassword = resultSet.getString(3); + UserEntity userEntity = new UserEntity(dbUserName, dbUserPassword); + return userEntity; + } catch (Exception e) { + e.printStackTrace(); + return null; + } finally { + JdbcUtils.closeConnection(resultSet, preparedStatement, connection); + } + } +} diff --git a/src/com/hellogithub/entity/AdminEntity.java b/src/com/hellogithub/entity/AdminEntity.java new file mode 100644 index 0000000..4684a9c --- /dev/null +++ b/src/com/hellogithub/entity/AdminEntity.java @@ -0,0 +1,4 @@ +package com.hellogithub.entity; + +public class AdminEntity { +} diff --git a/src/com/hellogithub/entity/UserEntity.java b/src/com/hellogithub/entity/UserEntity.java new file mode 100644 index 0000000..c81b28a --- /dev/null +++ b/src/com/hellogithub/entity/UserEntity.java @@ -0,0 +1,107 @@ +package com.hellogithub.entity; + +import java.sql.Date; + +/** + * 用户实体层 + */ +public class UserEntity { + /** + * CREATE TABLE `user` ( + * `userId` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID', + * `userName` varchar(255) DEFAULT NULL COMMENT '用户名', + * `userPassword` varchar(255) DEFAULT NULL COMMENT '密码', + * `creatTime` datetime DEFAULT NULL COMMENT '创建时间', + * `isValid` int(11) DEFAULT NULL COMMENT '是否有效(0/1)', + * `projectNum` int(11) DEFAULT NULL COMMENT '提交的项目总数', + * PRIMARY KEY (`userId`) + * ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COMMENT='用户表'; + */ + private Integer userId; + private String userName; + private String userPassword; + + private Date creatTime; + + private Integer isValid; + + private Integer projectNum; + + public UserEntity() { + + } + + public UserEntity(String userName, String userPassword) { + this.userName=userName; + this.userPassword=userPassword; + } + + public Integer getUserId() { + return userId; + } + + public void setUserId(Integer userId) { + this.userId = userId; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getUserPassword() { + return userPassword; + } + + public void setUserPassword(String userPassword) { + this.userPassword = userPassword; + } + + public Date getCreatTime() { + return creatTime; + } + + public void setCreatTime(Date creatTime) { + this.creatTime = creatTime; + } + + public Integer getIsValid() { + return isValid; + } + + public void setIsValid(Integer isValid) { + this.isValid = isValid; + } + + public Integer getProjectNum() { + return projectNum; + } + + public void setProjectNum(Integer projectNum) { + 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; + } + + @Override + public String toString() { + return "UserEntity{" + + "userId=" + userId + + ", userName='" + userName + '\'' + + ", userPassword='" + userPassword + '\'' + + ", creatTime=" + creatTime + + ", isValid=" + isValid + + ", projectNum=" + projectNum + + '}'; + } +} diff --git a/src/com/hellogithub/filter/SessionFilter.java b/src/com/hellogithub/filter/SessionFilter.java index 5ec511b..c399463 100644 --- a/src/com/hellogithub/filter/SessionFilter.java +++ b/src/com/hellogithub/filter/SessionFilter.java @@ -14,7 +14,7 @@ import java.io.IOException; */ @WebFilter("/system/*")// 过滤器所有的请求 public class SessionFilter implements Filter { - private String[] excludeUrls = new String[]{"/VerifycodeServlet"}; //过滤器排除的请求url 格式:”/login“ + private String[] excludeUrls = new String[]{"/VerifycodeServlet","/userLogin"}; //过滤器排除的请求url 格式:”/login“ /** * 过滤器 diff --git a/src/com/hellogithub/service/AdminService.java b/src/com/hellogithub/service/AdminService.java new file mode 100644 index 0000000..91fdfad --- /dev/null +++ b/src/com/hellogithub/service/AdminService.java @@ -0,0 +1,4 @@ +package com.hellogithub.service; + +public class AdminService { +} diff --git a/src/com/hellogithub/service/UserService.java b/src/com/hellogithub/service/UserService.java new file mode 100644 index 0000000..96c0b30 --- /dev/null +++ b/src/com/hellogithub/service/UserService.java @@ -0,0 +1,14 @@ +package com.hellogithub.service; + +import com.hellogithub.dao.UserDao; +import com.hellogithub.entity.UserEntity; + +/** + * 用户Service层 + * */ +public class UserService { + private UserDao userDao=new UserDao(); + public UserEntity UserLogin(String userName, String userPassword){ + return userDao.UserLogin(userName,userPassword); + } +} diff --git a/src/com/hellogithub/servlet/UserLoginServlet.java b/src/com/hellogithub/servlet/UserLoginServlet.java new file mode 100644 index 0000000..1fadae2 --- /dev/null +++ b/src/com/hellogithub/servlet/UserLoginServlet.java @@ -0,0 +1,86 @@ +package com.hellogithub.servlet; + +import com.alibaba.fastjson.JSONObject; +import com.hellogithub.entity.UserEntity; +import com.hellogithub.service.UserService; +import com.hellogithub.utils.RandomValidateCode; +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.*; +import org.apache.commons.lang3.StringUtils; + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.HashMap; + +@WebServlet("/userLogin") +public class UserLoginServlet extends HttpServlet { + private UserService userService = new UserService(); + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + doPost(req,resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + PrintWriter writer =resp.getWriter(); + String userName = req.getParameter("username"); + if (StringUtils.isEmpty(userName)) { + setResultError("用户名称不能够是为空",writer); + return; + } + String userPwd = req.getParameter("password"); + // 参数验证 + if (StringUtils.isEmpty(userPwd)) { + //转发到错误页面 + setResultError("密码不能够是为空",writer); + return; + } + String userCode = req.getParameter("code"); // 用户输入的图形验证码 + // 从session中获取图形验证码 + HttpSession session = req.getSession(); + String sessionCode = (String) session.getAttribute(RandomValidateCode.RANDOMVALIDATECODE); + if (!sessionCode.equalsIgnoreCase(userCode)) { + setResultError("验证码不能够是为空",writer); + return; + } + // 在调用业务逻辑层 + UserEntity adminUserEntity = userService.UserLogin(userName, userPwd); + if (adminUserEntity == null) { + // 用户名称或者密码错误! + setResultError("用户名或者密码错误!!!",writer); + return; + } + // 判断用户是否记住密码 + String rememberPassword = req.getParameter("remember"); + if ("on".equals(rememberPassword)) { + // 如果有记住密码则 将密码保存在cookie中 + Cookie userNameCookie = new Cookie("userName", userName); + Cookie userPwdCookie = new Cookie("userPwd", userPwd); + resp.addCookie(userNameCookie); + resp.addCookie(userPwdCookie); + } + // 能够db中查询到对象 登录成功了 将用户数据存放在session中 + session = req.getSession(); + session.setAttribute("user", adminUserEntity); + setResultOK("登录成功!",writer); + } + + public void setResult(Integer code, String msg, PrintWriter writer) { + HashMap result = new HashMap<>(); + result.put("code", code); + result.put("msg", msg); + String jsonString = JSONObject.toJSONString(result); + writer.println(jsonString); + writer.close(); + } + + public void setResultError(String msg, PrintWriter writer) { + setResult(500, msg, writer); + } + + public void setResultOK(String msg, PrintWriter writer) { + setResult(200, msg, writer); + } +}