This commit is contained in:
2023-06-09 01:59:08 +08:00
commit 23b265e957
37 changed files with 2087 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
package com.landaiqing.dao;
import com.landaiqing.entity.AdminUserEntity;
import com.landaiqing.utils.JdbcUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class AdminUserDao {
private AdminUserEntity adminUserEntity=new AdminUserEntity();
/**
* 管理员登录
* */
public AdminUserEntity login(String userName, String userPwd) {
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
Connection connection = null;
try {
connection = JdbcUtils.getConnection();
String loginSql = "select * from admin where adminUserName=? and adminPassword=?;";
preparedStatement = connection.prepareStatement(loginSql);
preparedStatement.setString(1, userName);
preparedStatement.setString(2, userPwd);
resultSet = preparedStatement.executeQuery();
if (!resultSet.next()) { // 查询不到用户数据
return null;
}
// 将db中数据 返回给客户端 查询到数据
Integer id = resultSet.getInt(1);
String dbUserName = resultSet.getString(2);
String dbUserPwd = resultSet.getString(3);
AdminUserEntity adminUserEntity = new AdminUserEntity(dbUserName, dbUserPwd);
return adminUserEntity;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
JdbcUtils.closeConnection(resultSet, preparedStatement, connection);
}
}
}

View File

@@ -0,0 +1,57 @@
package com.landaiqing.entity;
public class AdminUserEntity {
/**
*CREATE TABLE `admin-user` (
* `adminId` int NOT NULL AUTO_INCREMENT COMMENT 'ID',
* `adminUserName` varchar(25) NOT NULL COMMENT '管理员账号',
* `adminPassword` varchar(255) DEFAULT NULL COMMENT '管理员密码',
* PRIMARY KEY (`adminId`) USING BTREE
* ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
*/
private Integer adminId;
private String adminUserName;
private String adminPassword;
public Integer getAdminId() {
return adminId;
}
public void setAdminId(Integer adminId) {
this.adminId = adminId;
}
public String getAdminUserName() {
return adminUserName;
}
public void setAdminUserName(String adminUserName) {
this.adminUserName = adminUserName;
}
public String getAdminPassword() {
return adminPassword;
}
public void setAdminPassword(String adminPassword) {
this.adminPassword = adminPassword;
}
public AdminUserEntity(Integer adminId, String adminUserName, String adminPassword) {
this.adminId = adminId;
this.adminUserName = adminUserName;
this.adminPassword = adminPassword;
}
public AdminUserEntity(String adminUserName, String adminPassword) {
this.adminUserName = adminUserName;
this.adminPassword = adminPassword;
}
public AdminUserEntity(){
}
}

View File

@@ -0,0 +1,48 @@
//package com.landaiqing.filter;
//
//
//import jakarta.servlet.*;
//import jakarta.servlet.annotation.WebFilter;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import jakarta.servlet.http.HttpSession;
//
//import java.io.IOException;
//
///**
// * 过滤器
// */
//@WebFilter("/*")// 过滤器所有的请求
//public class UserSessionFilter implements Filter {
// private String[] excludeUrls = new String[]{"/login", "/register", "/VerifycodeServlet"};
//
// @Override
// public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
// // 从session获取到用户的会话信息 判断用户是否登录过
// HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
// HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
// String contextPath = httpServletRequest.getContextPath();
// // 定义一个数组 哪些 请求是需要排除的
// for (int i = 0; i < excludeUrls.length; i++) {
// String excludeUrl = contextPath + excludeUrls[i];
// String requestURI = httpServletRequest.getRequestURI();
// if (excludeUrl.equals(requestURI)) {
// // 放行请求
// filterChain.doFilter(httpServletRequest, httpServletResponse);
// return;
// }
// }
// // 排除请求
// HttpSession session = httpServletRequest.getSession();
// Object user = session.getAttribute("user");
// if (user == null) {
// // 当前用户没有登录或者登录会话失效
// // 重定向到登录页面
// httpServletResponse.sendRedirect(contextPath+"/login");
// return;
// }
// // 用户已经登录了 正常放行请求
// filterChain.doFilter(httpServletRequest, httpServletResponse);
// }
//}

View File

@@ -0,0 +1,15 @@
package com.landaiqing.service;
import com.landaiqing.dao.AdminUserDao;
import com.landaiqing.entity.AdminUserEntity;
public class AdminUserService {
private AdminUserDao adminUserDao=new AdminUserDao();
/**
* 管理员登录
* */
public AdminUserEntity login(String userName, String userPwd) {
return adminUserDao.login(userName,userPwd);
}
}

View File

@@ -0,0 +1,80 @@
package com.landaiqing.servlet;
import com.landaiqing.entity.AdminUserEntity;
import com.landaiqing.service.AdminUserService;
import com.landaiqing.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;
/**
* 管理员登录
*/
@WebServlet("/login")
public class AdminLoginServlet extends HttpServlet {
private AdminUserService adminUserService = new AdminUserService();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 转发login页面
req.getRequestDispatcher("login.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 点击登录的时候 获取到用户的参数
String userName = req.getParameter("username");
if (StringUtils.isEmpty(userName)) {
//转发到错误页面
req.setAttribute("errorMsg", "用户名称不能够是为空!");
req.getRequestDispatcher("error.jsp").forward(req, resp);
return;
}
String userPwd = req.getParameter("password");
// 参数验证
if (StringUtils.isEmpty(userPwd)) {
//转发到错误页面
req.setAttribute("errorMsg", "userPwd不能够是为空!");
req.getRequestDispatcher("error.jsp").forward(req, resp);
return;
}
String userCode = req.getParameter("code"); // 用户输入的图形验证码
// 从session中获取图形验证码
HttpSession session = req.getSession();
String sessionCode = (String) session.getAttribute(RandomValidateCode.RANDOMVALIDATECODE);
if (!sessionCode.equalsIgnoreCase(userCode)) {
req.setAttribute("errorMsg", "图形验证码不正确,请重新输入!");
req.getRequestDispatcher("error.jsp").forward(req, resp);
return;
}
// 在调用业务逻辑层
AdminUserEntity adminUserEntity = adminUserService.login(userName, userPwd);
if (adminUserEntity == null) {
// 用户名称或者密码错误!
req.setAttribute("errorMsg", "用户名称或者是密码错误!");
req.getRequestDispatcher("error.jsp").forward(req, resp);
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);
// 在转发到首页(重定向到首页)
// req.getRequestDispatcher("index.jsp").forward(req, resp);
resp.sendRedirect("./System/index.jsp");
}
}

View File

@@ -0,0 +1,39 @@
package com.landaiqing.servlet;
import com.landaiqing.utils.RandomValidateCode;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 前台验证码处点击刷新,发送到该servlet的请求,
* 该servlet调用生成验证码的工具类返回一个图像验证码
*/
@WebServlet(name = "VerifycodeServlet", urlPatterns = "/VerifycodeServlet")
public class VerifycodeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
response.setHeader("Pragma", "No-cache");//设置响应头信息,告诉浏览器不要缓存此内容
//做浏览器兼容
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expire", 0);
RandomValidateCode randomValidateCode = new RandomValidateCode();
try {
randomValidateCode.getRandcode(request, response);//输出图片方法
} catch (Exception e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}

View File

@@ -0,0 +1,132 @@
package com.landaiqing.utils;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JdbcUtils {
private JdbcUtils() {
}
/**
* 2.定义工具类 需要 声明 变量
*/
private static String driverClass;
private static String url;
private static String user;
private static String password;
/**
*3.使用静态代码快 来给我们声明好 jdbc变量赋值读取config.properties
*/
static {
try {
// 1.读取config.properties IO 路径 相对路径
InputStream resourceAsStream = JdbcUtils.class.getClassLoader().
getResourceAsStream("config.properties");
// 2.赋值给我们声明好的变量
Properties properties = new Properties();
properties.load(resourceAsStream);
driverClass = properties.getProperty("driverClass");
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("password");
// 3.注册驱动类
Class.forName(driverClass);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 4.封装连接方法
*/
public static Connection getConnection() throws SQLException {
Connection connection = DriverManager.getConnection(url, user, password);
return connection;
}
/**
* 5.封装释放连接方法 (重载)
*/
public static void closeConnection(ResultSet resultSet, Statement statement, Connection connection) {
// 1.查询 释放连接 resultSet statement connection
try {
if (resultSet != null)
resultSet.close();
if (statement != null)
statement.close();
if (connection != null)
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 增删改---释放jdbc资源
*
* @param statement
* @param connection
*/
public static void closeConnection(Statement statement, Connection connection) {
// 1.查询 释放连接 resultSet statement connection
closeConnection(null, statement, connection);
}
/**
* 开启事务
*
* @param connection
* @throws SQLException
*/
public static void beginTransaction(Connection connection) throws SQLException {
connection.setAutoCommit(false);
}
/**
* 提交事务
*
* @param connection
* @throws SQLException
*/
public static void commitTransaction(Connection connection) throws SQLException {
connection.commit();
}
/**
* 回滚事务
*
* @param connection
*/
public static void rollBackTransaction(Connection connection) {
if (connection != null) {
try {
connection.rollback();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 关闭事务
*
* @param connection
*/
public static void endTransaction(Connection connection) {
if (connection != null) {
try {
connection.setAutoCommit(true);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

View File

@@ -0,0 +1,105 @@
package com.landaiqing.utils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
/**
* 工具类,生成随机验证码
*/
public class RandomValidateCode {
public static final String RANDOMVALIDATECODE = "RandomValidateCode";// 放到session中的key
private Random random = new Random();
private String randString = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";//随机产生的字符串
private int width = 100;// 图片宽度
private int height = 26;// 图片高度
private int lineSize = 40;// 干扰线数量
private int stringNum = 4;// 随机产生的字符数量
/**
* 获得字体
*/
private Font getFont() {
return new Font("Fixedsys", Font.CENTER_BASELINE, 18);
}
/**
* 获得颜色
*/
private Color getRandColor(int fc, int bc) {
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc - 16);
int g = fc + random.nextInt(bc - fc - 14);
int b = fc + random.nextInt(bc - fc - 18);
return new Color(r, g, b);
}
/**
* 生成随机图片
*/
public void getRandcode(HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession();
// BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
Graphics g = image.getGraphics();// 产生Image对象的Graphics对象,该对象可以在图像上进行各种绘制操作
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman", Font.ROMAN_BASELINE, 18));
g.setColor(getRandColor(110, 133));
// 绘制干扰线
for (int i = 0; i <= lineSize; i++) {
drowLine(g);
}
// 绘制随机字符
String randomString = "";
for (int i = 1; i <= stringNum; i++) {
randomString = drowString(g, randomString, i);
}
session.removeAttribute(RANDOMVALIDATECODE);
session.setAttribute(RANDOMVALIDATECODE, randomString);
g.dispose();
try {
ImageIO.write(image, "JPEG", response.getOutputStream());// 将内存中的图片通过流动形式输出到客户端
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 绘制字符串
*/
private String drowString(Graphics g, String randomString, int i) {
g.setFont(getFont());
g.setColor(new Color(random.nextInt(101), random.nextInt(111), random.nextInt(121)));
String rand = getRandomString(random.nextInt(randString.length()));
randomString += rand;
g.translate(random.nextInt(3), random.nextInt(3));
g.drawString(rand, 13 * i, 16);
return randomString;
}
/**
* 绘制干扰线
*/
private void drowLine(Graphics g) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(13);
int yl = random.nextInt(15);
g.drawLine(x, y, x + xl, y + yl);
}
/**
* 获取随机的字符
*/
public String getRandomString(int num) {
return String.valueOf(randString.charAt(num));
}
}

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/webguestbook?serverTimezone=GMT%2B8
user=root
password=1611