feat: admin login

This commit is contained in:
xtaodada 2024-05-12 20:10:11 +08:00
parent 6757125a4e
commit 217dd9f137
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
8 changed files with 353 additions and 1 deletions

View File

@ -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 "";
}
}

View File

@ -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<AdminUser> findAll();
AdminUser findByAdminname(String adminname);
int deleteByAdminname(String adminname);
int passModify(String adminname, String adminpass);
int add(AdminUser adminUser);
}

View File

@ -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<AdminUser> findAll() {
return (ArrayList<AdminUser>) 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);
}
}

View File

@ -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<AdminUser> findAll();
public AdminUser findByAdminname(String adminname);
public int deleteByAdminname(String adminname);
public int passModify(String adminname, String adminpass);
}

View File

@ -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<AdminUser> 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;
}
}

View File

@ -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();
}
}

View File

@ -12,7 +12,7 @@
<context:component-scan base-package="cn.edu.cqwu.repair"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="WEB-INF/pages/" />
<property name="prefix" value="" />
<property name="suffix" value=".jsp" />
</bean>

View File

@ -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" %>
<c:set var="webroot" value="${pageContext.request.contextPath}"/>
<c:set var="title" value="管理员登录"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>${title}</title>
<link rel="stylesheet" href="${webroot}/styles/bootstrap.min.css" crossorigin="anonymous">
<script src="${webroot}/js/bootstrap.bundle.min.js"></script>
<link rel="stylesheet" href="${webroot}/styles/sidebar.css" crossorigin="anonymous">
<link rel="stylesheet" href="${webroot}/styles/index.css" crossorigin="anonymous">
</head>
<body>
<main class="d-flex flex-nowrap">
<%@ include file="./includes/header.jsp" %>
<main class="form-signin w-100 m-auto">
<div class="toast-container position-fixed bottom-0 end-0 p-3">
<div id="liveToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<img src="${webroot}/images/logo.jpg" class="rounded me-2" width="32" height="32">
<strong class="me-auto">提示</strong>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
</div>
<div class="toast-body">
1、请牢记您注册时填写的用户名和密码登录本系统时您需要提供正确的用户名和密码</br>
2、忘记用户名或者密码请联系系统管理员
</div>
</div>
</div>
<form action="${webroot}/adminLogin.do" method="post">
<img class="mb-4" src="${webroot}/images/logo.jpg" alt="" width="72" height="72">
<h1 class="h3 mb-3 fw-normal">管理员登录</h1>
<c:if test="${not empty requestScope.adminLoginMess}">
<div class="alert alert-warning" role="alert">
${requestScope.adminLoginMess}
</div>
</c:if>
<div class="form-floating">
<input type="text" class="form-control" name="username" id="username" value="${username}" required>
<label for="username">用户名</label>
</div>
<div class="form-floating">
<input type="password" class="form-control" name="password" id="password" value="${password}" required>
<label for="password">密码</label>
</div>
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="验证码" name="code" id="code" required>
<span class="input-group-text">
<img src="${pageContext.request.contextPath}/includes/code.jsp" id="imagecode"
title="点击图片可刷新验证码"
onclick="this.src='${pageContext.request.contextPath}/includes/code.jsp?'+Math.random()">
</span>
</div>
<input class="btn btn-primary w-100 py-2" type="submit" id="submit">
<br/><br/>
</form>
</main>
</main>
<script>
const toastLiveExample = document.getElementById('liveToast')
const toastBootstrap = bootstrap.Toast.getOrCreateInstance(toastLiveExample)
toastBootstrap.show()
</script>
</body>
</html>