commit 98e140e9fa6b4892c582680d822ed4e40a9d2081 Author: Qing Date: Tue Jun 27 16:18:38 2023 +0800 Init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8eb6870 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ +.idea +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/helloGithub.iml b/helloGithub.iml new file mode 100644 index 0000000..b4c3af3 --- /dev/null +++ b/helloGithub.iml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/libs/commons-dbutils-1.7.jar b/libs/commons-dbutils-1.7.jar new file mode 100644 index 0000000..b30458e Binary files /dev/null and b/libs/commons-dbutils-1.7.jar differ diff --git a/libs/commons-lang3-3.12.0.jar b/libs/commons-lang3-3.12.0.jar new file mode 100644 index 0000000..4d434a2 Binary files /dev/null and b/libs/commons-lang3-3.12.0.jar differ diff --git a/libs/fastjson-1.2.79.jar b/libs/fastjson-1.2.79.jar new file mode 100644 index 0000000..63f5d50 Binary files /dev/null and b/libs/fastjson-1.2.79.jar differ diff --git a/libs/jakarta.servlet.jsp.jstl-2.0.0.jar b/libs/jakarta.servlet.jsp.jstl-2.0.0.jar new file mode 100644 index 0000000..92712b0 Binary files /dev/null and b/libs/jakarta.servlet.jsp.jstl-2.0.0.jar differ diff --git a/libs/jakarta.servlet.jsp.jstl-api-2.0.0.jar b/libs/jakarta.servlet.jsp.jstl-api-2.0.0.jar new file mode 100644 index 0000000..81059ec Binary files /dev/null and b/libs/jakarta.servlet.jsp.jstl-api-2.0.0.jar differ diff --git a/libs/mysql-connector-j-8.0.31.jar b/libs/mysql-connector-j-8.0.31.jar new file mode 100644 index 0000000..8b74bb8 Binary files /dev/null and b/libs/mysql-connector-j-8.0.31.jar differ diff --git a/libs/servlet-api.jar b/libs/servlet-api.jar new file mode 100644 index 0000000..1321455 Binary files /dev/null and b/libs/servlet-api.jar differ diff --git a/src/com/hellogithub/filter/SessionFilter.java b/src/com/hellogithub/filter/SessionFilter.java new file mode 100644 index 0000000..0599521 --- /dev/null +++ b/src/com/hellogithub/filter/SessionFilter.java @@ -0,0 +1,56 @@ +package com.hellogithub.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("/system/*")// 过滤器所有的请求 +public class SessionFilter implements Filter { + private String[] excludeUrls = new String[]{""}; //过滤器排除的请求url 格式:”/login“ + + /** + * 过滤器 + * + * @param servletRequest + * @param servletResponse + * @param filterChain + * @throws IOException,ServletException + */ + @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 + "/"); + return; + } + // 用户已经登录了 正常放行请求 + filterChain.doFilter(httpServletRequest, httpServletResponse); + } +} + diff --git a/src/com/hellogithub/utils/JdbcUtils.java b/src/com/hellogithub/utils/JdbcUtils.java new file mode 100644 index 0000000..b8f3611 --- /dev/null +++ b/src/com/hellogithub/utils/JdbcUtils.java @@ -0,0 +1,145 @@ +package com.hellogithub.utils; + + +import java.io.InputStream; +import java.sql.*; +import java.util.Properties; + +/** + * 工具类:jdbc + * */ +public class JdbcUtils { + + private JdbcUtils() { + + } + + /** + * 声明变量 + * driverClass : jdbc驱动 + * url : jdbc链接url + * user : 数据库用户名 + * password : 数据库密码 + */ + private static String driverClass; + private static String url; + private static String user; + private static String password; + + /** + *2.使用静态代码快 给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(); + } + } + + /** + * 3.封装连接方法 + * + * @throws SQLException + */ + public static Connection getConnection() throws SQLException { + Connection connection = DriverManager.getConnection(url, user, password); + return connection; + } + + /** + * 3.封装释放连接方法 (重载) + * + * @param resultSet + * @param statement + * @param connection + */ + 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(); + } + + } + + /** + * 4.增删改---释放jdbc资源 + * + * @param statement + * @param connection + */ + public static void closeConnection(Statement statement, Connection connection) { + // 1.查询 释放连接 resultSet statement connection + closeConnection(null, statement, connection); + } + + /** + * 5.开启事务 + * + * @param connection + * @throws SQLException + */ + public static void beginTransaction(Connection connection) throws SQLException { + connection.setAutoCommit(false); + } + + /** + * 6.提交事务 + * + * @param connection + * @throws SQLException + */ + public static void commitTransaction(Connection connection) throws SQLException { + connection.commit(); + } + + /** + * 7.回滚事务 + * + * @param connection + */ + public static void rollBackTransaction(Connection connection) { + if (connection != null) { + try { + connection.rollback(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + + /** + * 8.关闭事务 + * + * @param connection + */ + public static void endTransaction(Connection connection) { + if (connection != null) { + try { + connection.setAutoCommit(true); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + +} + diff --git a/src/com/hellogithub/utils/RandomValidateCode.java b/src/com/hellogithub/utils/RandomValidateCode.java new file mode 100644 index 0000000..f115e4f --- /dev/null +++ b/src/com/hellogithub/utils/RandomValidateCode.java @@ -0,0 +1,119 @@ +package com.hellogithub.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); + } + + /** + * 获得颜色 + * + * @param bc + * @param fc + */ + 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); + } + + /** + * 生成随机图片 + * + * @param request + * @param response + */ + 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(); + } + } + + /** + * 绘制字符串 + * + * @param g + * @param randomString + * @param i + */ + 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; + } + + /** + * 绘制干扰线 + * + * @param g + */ + 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); + } + + /** + * 获取随机的字符 + * + * @param num + */ + public String getRandomString(int num) { + return String.valueOf(randString.charAt(num)); + } +} diff --git a/web/WEB-INF/web.xml b/web/WEB-INF/web.xml new file mode 100644 index 0000000..d80081d --- /dev/null +++ b/web/WEB-INF/web.xml @@ -0,0 +1,6 @@ + + + \ No newline at end of file diff --git a/web/index.jsp b/web/index.jsp new file mode 100644 index 0000000..136da64 --- /dev/null +++ b/web/index.jsp @@ -0,0 +1,16 @@ +<%-- + Created by IntelliJ IDEA. + User: LDQ + Date: 2023/6/27 + Time: 15:31 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + $Title$ + + + $END$ + +