From 217dd9f13765882987e33fc629ed41f28fdb1901 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Sun, 12 May 2024 20:10:11 +0800 Subject: [PATCH] feat: admin login --- .../admin/AdminLoginController.java | 64 +++++++++++++++ .../cn/edu/cqwu/repair/dao/AdminUserDao.java | 22 +++++ .../repair/dao/impl/AdminUserDaoImpl.java | 64 +++++++++++++++ .../cqwu/repair/service/AdminUserService.java | 19 +++++ .../service/impl/AdminUserServiceImpl.java | 52 ++++++++++++ .../java/cn/edu/cqwu/repair/util/Encrypt.java | 49 +++++++++++ src/main/resources/springmvc.xml | 2 +- src/main/webapp/manage.jsp | 82 +++++++++++++++++++ 8 files changed, 353 insertions(+), 1 deletion(-) create mode 100644 src/main/java/cn/edu/cqwu/repair/controller/admin/AdminLoginController.java create mode 100644 src/main/java/cn/edu/cqwu/repair/dao/AdminUserDao.java create mode 100644 src/main/java/cn/edu/cqwu/repair/dao/impl/AdminUserDaoImpl.java create mode 100644 src/main/java/cn/edu/cqwu/repair/service/AdminUserService.java create mode 100644 src/main/java/cn/edu/cqwu/repair/service/impl/AdminUserServiceImpl.java create mode 100644 src/main/java/cn/edu/cqwu/repair/util/Encrypt.java create mode 100644 src/main/webapp/manage.jsp diff --git a/src/main/java/cn/edu/cqwu/repair/controller/admin/AdminLoginController.java b/src/main/java/cn/edu/cqwu/repair/controller/admin/AdminLoginController.java new file mode 100644 index 0000000..2fa6626 --- /dev/null +++ b/src/main/java/cn/edu/cqwu/repair/controller/admin/AdminLoginController.java @@ -0,0 +1,64 @@ +package cn.edu.cqwu.repair.controller.admin; + +import cn.edu.cqwu.repair.entity.AdminUser; +import cn.edu.cqwu.repair.service.AdminUserService; +import cn.edu.cqwu.repair.util.Encrypt; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +import javax.servlet.http.HttpSession; + +/** + * @author xtaod + */ +@Controller +public class AdminLoginController { + private final AdminUserService adminUserService; + + @Autowired + public AdminLoginController(AdminUserService adminUserService) { + this.adminUserService = adminUserService; + } + + @RequestMapping("/adminLogin.do") + public String adminLogin(String username, String password, String code, Model model, HttpSession session) { + String mess = validateForm(username, password, code); + if (!mess.isEmpty()) { + model.addAttribute("adminLoginMess", mess); + return "manage"; + } + if (!verifyCode(session, code)) { + model.addAttribute("adminLoginMess", "* 验证码错误!"); + return "manage"; + } + AdminUser user = adminUserService.validateLogin(username, Encrypt.SHA(password)); + if (user == null) { + model.addAttribute("adminLoginMess", "* 用户名或密码输入错误!"); + return "manage"; + } else { + session.setAttribute("adminuser", user); + return "redirect:/admin/state.jsp"; + } + } + + public boolean verifyCode(HttpSession session, String code) { + String sessioncode = session.getAttribute("sessioncode").toString(); + if (!code.equals(sessioncode)) { + return false; + } + return true; + } + + private String validateForm(String username, String password, String code) { + if (username == null || !username.matches("\\w{5,20}")) { + return "* 用户名不合法!"; + } else if (password == null || !password.matches("\\w{5,20}")) { + return "* 密码不合法!"; + } else if (code == null || !code.matches("\\d{4}")) { + return "* 验证码错误!"; + } + return ""; + } +} diff --git a/src/main/java/cn/edu/cqwu/repair/dao/AdminUserDao.java b/src/main/java/cn/edu/cqwu/repair/dao/AdminUserDao.java new file mode 100644 index 0000000..6ed29e7 --- /dev/null +++ b/src/main/java/cn/edu/cqwu/repair/dao/AdminUserDao.java @@ -0,0 +1,22 @@ +package cn.edu.cqwu.repair.dao; + +import cn.edu.cqwu.repair.entity.AdminUser; + +import java.util.ArrayList; + +/** + * @author xtaod + */ +public interface AdminUserDao { + AdminUser validateLogin(String username, String password); + + ArrayList findAll(); + + AdminUser findByAdminname(String adminname); + + int deleteByAdminname(String adminname); + + int passModify(String adminname, String adminpass); + + int add(AdminUser adminUser); +} diff --git a/src/main/java/cn/edu/cqwu/repair/dao/impl/AdminUserDaoImpl.java b/src/main/java/cn/edu/cqwu/repair/dao/impl/AdminUserDaoImpl.java new file mode 100644 index 0000000..431eeef --- /dev/null +++ b/src/main/java/cn/edu/cqwu/repair/dao/impl/AdminUserDaoImpl.java @@ -0,0 +1,64 @@ +package cn.edu.cqwu.repair.dao.impl; + +import cn.edu.cqwu.repair.dao.AdminUserDao; +import cn.edu.cqwu.repair.db.ConnectionFactory; +import cn.edu.cqwu.repair.entity.AdminUser; +import cn.edu.cqwu.repair.entity.mapper.AdminUserMapper; +import com.mybatisflex.core.query.QueryWrapper; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; + +import static cn.edu.cqwu.repair.entity.table.AdminUserTableDef.ADMIN_USER; + +/** + * @author xtaod + */ +@Component +public class AdminUserDaoImpl implements AdminUserDao { + private static final AdminUserMapper mapper = ConnectionFactory.getMapper(AdminUserMapper.class); + + @Override + public AdminUser validateLogin(String username, String password) { + QueryWrapper qw = new QueryWrapper(); + qw.select(ADMIN_USER.ALL_COLUMNS) + .where(ADMIN_USER.ADMIN_NAME.eq(username)) + .and(ADMIN_USER.ADMIN_PASS.eq(password)); + return mapper.selectOneByQuery(qw); + } + + @Override + public ArrayList findAll() { + return (ArrayList) mapper.selectAll(); + } + + @Override + public AdminUser findByAdminname(String adminname) { + QueryWrapper qw = new QueryWrapper(); + qw.select(ADMIN_USER.ALL_COLUMNS) + .where(ADMIN_USER.ADMIN_NAME.eq(adminname)); + return mapper.selectOneByQuery(qw); + } + + @Override + public int deleteByAdminname(String adminname) { + QueryWrapper qw = new QueryWrapper(); + qw.where(ADMIN_USER.ADMIN_NAME.eq(adminname)); + return mapper.deleteByQuery(qw); + } + + @Override + public int passModify(String adminname, String adminpass) { + AdminUser user = findByAdminname(adminname); + if (user == null) { + return 0; + } + user.setAdminPass(adminpass); + return mapper.update(user); + } + + @Override + public int add(AdminUser adminUser) { + return mapper.insert(adminUser); + } +} diff --git a/src/main/java/cn/edu/cqwu/repair/service/AdminUserService.java b/src/main/java/cn/edu/cqwu/repair/service/AdminUserService.java new file mode 100644 index 0000000..31e9974 --- /dev/null +++ b/src/main/java/cn/edu/cqwu/repair/service/AdminUserService.java @@ -0,0 +1,19 @@ +package cn.edu.cqwu.repair.service; + +import cn.edu.cqwu.repair.entity.AdminUser; + +import java.util.ArrayList; + +public interface AdminUserService { + public int add(AdminUser adminUser); + + public AdminUser validateLogin(String username, String password); + + public ArrayList findAll(); + + public AdminUser findByAdminname(String adminname); + + public int deleteByAdminname(String adminname); + + public int passModify(String adminname, String adminpass); +} diff --git a/src/main/java/cn/edu/cqwu/repair/service/impl/AdminUserServiceImpl.java b/src/main/java/cn/edu/cqwu/repair/service/impl/AdminUserServiceImpl.java new file mode 100644 index 0000000..5470726 --- /dev/null +++ b/src/main/java/cn/edu/cqwu/repair/service/impl/AdminUserServiceImpl.java @@ -0,0 +1,52 @@ +package cn.edu.cqwu.repair.service.impl; + +import cn.edu.cqwu.repair.dao.AdminUserDao; +import cn.edu.cqwu.repair.entity.AdminUser; +import cn.edu.cqwu.repair.service.AdminUserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; + +/** + * @author xtaod + */ +@Service +public class AdminUserServiceImpl implements AdminUserService { + private final AdminUserDao adminUserDao; + + @Autowired + public AdminUserServiceImpl(AdminUserDao adminUserDao) { + this.adminUserDao = adminUserDao; + } + + @Override + public int add(AdminUser adminUser) { + return 0; + } + + @Override + public AdminUser validateLogin(String username, String password) { + return adminUserDao.validateLogin(username, password); + } + + @Override + public ArrayList findAll() { + return null; + } + + @Override + public AdminUser findByAdminname(String adminname) { + return null; + } + + @Override + public int deleteByAdminname(String adminname) { + return 0; + } + + @Override + public int passModify(String adminname, String adminpass) { + return 0; + } +} diff --git a/src/main/java/cn/edu/cqwu/repair/util/Encrypt.java b/src/main/java/cn/edu/cqwu/repair/util/Encrypt.java new file mode 100644 index 0000000..67f57c2 --- /dev/null +++ b/src/main/java/cn/edu/cqwu/repair/util/Encrypt.java @@ -0,0 +1,49 @@ +package cn.edu.cqwu.repair.util; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; + +/** + * @author xtaod + */ +public class Encrypt { + public static String MD5(String s) { + char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; + try { + MessageDigest mdInst = MessageDigest.getInstance("MD5"); + mdInst.update(s.getBytes()); + byte[] md = mdInst.digest(); + int k = 0, j = md.length; + char[] str = new char[j * 2]; + for (byte byte0 : md) { + str[k++] = hexDigits[byte0 >>> 4 & 0xf]; + str[k++] = hexDigits[byte0 & 0xf]; + } + return new String(str); + } catch (Exception e) { + e.printStackTrace(); + return ""; + } + } + + public static String SHA(String s) { + MessageDigest sha; + StringBuilder hexValue; + try { + sha = MessageDigest.getInstance("SHA"); + byte[] md5Bytes = sha.digest(s.getBytes(StandardCharsets.UTF_8)); + hexValue = new StringBuilder(); + for (byte md5Byte : md5Bytes) { + int val = ((int) md5Byte) & 0xff; + if (val < 16) { + hexValue.append("0"); + } + hexValue.append(Integer.toHexString(val)); + } + } catch (Exception e) { + e.printStackTrace(); + return ""; + } + return hexValue.toString(); + } +} diff --git a/src/main/resources/springmvc.xml b/src/main/resources/springmvc.xml index 994bfa1..c906a51 100644 --- a/src/main/resources/springmvc.xml +++ b/src/main/resources/springmvc.xml @@ -12,7 +12,7 @@ - + diff --git a/src/main/webapp/manage.jsp b/src/main/webapp/manage.jsp new file mode 100644 index 0000000..75c88ae --- /dev/null +++ b/src/main/webapp/manage.jsp @@ -0,0 +1,82 @@ +<%-- + Created by IntelliJ IDEA. + User: xtaod + Date: 2023/12/9 + Time: 10:18 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> +<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> +<%@ page isELIgnored="false" %> + + + + + + + ${title} + + + + + + +
+ <%@ include file="./includes/header.jsp" %> + +
+
+ +
+
+ +

管理员登录

+ + + + + +
+ + +
+
+ + +
+
+ + + + +
+ + +

+
+
+
+ + +