• 三、Maven-单一架构案例(搭建环境:辅助功能,业务功能:登录)



    第五节 搭建环境:辅助功能

    1、常量类

    images

    public class ImperialCourtConst {
    
        public static final String LOGIN_FAILED_MESSAGE = "账号、密码错误,不可进宫!";
        public static final String ACCESS_DENIED_MESSAGE = "宫闱禁地,不得擅入!";
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2、MD5 加密工具方法

    images

    public class MD5Util {
    
        /**
         * 针对明文字符串执行MD5加密
         * @param source
         * @return
         */
        public static String encode(String source) {
    
            // 1.判断明文字符串是否有效
            if (source == null || "".equals(source)) {
                throw new RuntimeException("用于加密的明文不可为空");
            }
    
            // 2.声明算法名称
            String algorithm = "md5";
    
            // 3.获取MessageDigest对象
            MessageDigest messageDigest = null;
            try {
                messageDigest = MessageDigest.getInstance(algorithm);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
    
            // 4.获取明文字符串对应的字节数组
            byte[] input = source.getBytes();
    
            // 5.执行加密
            byte[] output = messageDigest.digest(input);
    
            // 6.创建BigInteger对象
            int signum = 1;
            BigInteger bigInteger = new BigInteger(signum, output);
    
            // 7.按照16进制将bigInteger的值转换为字符串
            int radix = 16;
            String encoded = bigInteger.toString(radix).toUpperCase();
    
            return encoded;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    3、日志配置文件

    images

    
    
    <configuration debug="true">
        
        <appender name="STDOUT"
                  class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                
                
                <pattern>[%d{HH:mm:ss.SSS}] [%-5level] [%thread] [%logger] [%msg]%npattern>
                <charset>UTF-8charset>
            encoder>
        appender>
    
        
        
        <root level="INFO">
            
            <appender-ref ref="STDOUT" />
        root>
    
        
        <logger name="com.atguigu" level="DEBUG" additivity="false">
            <appender-ref ref="STDOUT" />
        logger>
    
    configuration>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    第六节 业务功能:登录

    1、显示首页

    ①流程图

    images

    ②创建 PortalServlet

    [1]创建 Java 类

    images

    public class PortalServlet extends ViewBaseServlet {
    
        @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 {
            // 声明要访问的首页的逻辑视图
            String templateName = "index";
            
            // 调用父类的方法根据逻辑视图名称渲染视图
            processTemplate(templateName, req, resp);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    [2]注册

    images

    <servlet>
        <servlet-name>portalServletservlet-name>
        <servlet-class>com.atguigu.imperial.court.servlet.module.PortalServletservlet-class>
    servlet>
    <servlet-mapping>
        <servlet-name>portalServletservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    ③在 index.html 中编写登录表单

    iamgs

    DOCTYPE html>
    <html lang="en" xml:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
    
    <form th:action="@{/auth}" method="post">
        
        <input type="hidden" name="method" value="login" />
    
        
        
        <p th:text="${message}">p>
        <p th:text="${systemMessage}">p>
        
        账号:<input type="text" name="loginAccount"/><br/>
        密码:<input type="password" name="loginPassword"><br/>
        <button type="submit">进宫button>
    form>
    
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    2、登录操作

    ①流程图

    images

    ②创建 EmpService

    images

    ③创建登录失败异常

    images

    public class LoginFailedException extends RuntimeException {
    
        public LoginFailedException() {
        }
    
        public LoginFailedException(String message) {
            super(message);
        }
    
        public LoginFailedException(String message, Throwable cause) {
            super(message, cause);
        }
    
        public LoginFailedException(Throwable cause) {
            super(cause);
        }
    
        public LoginFailedException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
            super(message, cause, enableSuppression, writableStackTrace);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    ④增加常量声明

    images

    public class ImperialCourtConst {
    
        public static final String LOGIN_FAILED_MESSAGE = "账号、密码错误,不可进宫!";
        public static final String ACCESS_DENIED_MESSAGE = "宫闱禁地,不得擅入!";
        public static final String LOGIN_EMP_ATTR_NAME = "loginInfo";
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    ⑤创建 AuthServlet

    [1]创建 Java 类

    images

    
    public class AuthServlet extends ModelBaseServlet {
    
        private EmpService empService = new EmpServiceImpl();
    
        protected void login(
                HttpServletRequest request,
                HttpServletResponse response)
                throws ServletException, IOException {
    
            try {
                // 1、获取请求参数
                String loginAccount = request.getParameter("loginAccount");
                String loginPassword = request.getParameter("loginPassword");
    
                // 2、调用 EmpService 方法执行登录逻辑
                Emp emp = empService.getEmpByLoginAccount(loginAccount, loginPassword);
    
                // 3、通过 request 获取 HttpSession 对象
                HttpSession session = request.getSession();
    
                // 4、将查询到的 Emp 对象存入 Session 域
                session.setAttribute(ImperialCourtConst.LOGIN_EMP_ATTR_NAME, emp);
    
                // 5、前往指定页面视图
                String templateName = "temp";
                processTemplate(templateName, request, response);
    
            } catch (Exception e) {
                e.printStackTrace();
    
                // 6、判断此处捕获到的异常是否是登录失败异常
                if (e instanceof LoginFailedException) {
                    // 7、如果是登录失败异常则跳转回登录页面
                    // ①将异常信息存入请求域
                    request.setAttribute("message", e.getMessage());
    
                    // ②处理视图:index
                    processTemplate("index", request, response);
    
                }else {
                    // 8、如果不是登录异常则封装为运行时异常继续抛出
                    throw new RuntimeException(e);
    
                }
    
            }
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    [2]注册

    images

    <servlet>
        <servlet-name>authServletservlet-name>
        <servlet-class>com.atguigu.imperial.court.servlet.module.AuthServletservlet-class>
    servlet>
    <servlet-mapping>
        <servlet-name>authServletservlet-name>
        <url-pattern>/authurl-pattern>
    servlet-mapping>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    ⑥EmpService 方法

    images

    public class EmpServiceImpl implements EmpService {
    
        private EmpDao empDao = new EmpDaoImpl();
    
        @Override
        public Emp getEmpByLoginAccount(String loginAccount, String loginPassword) {
    
            // 1、对密码执行加密
            String encodedLoginPassword = MD5Util.encode(loginPassword);
    
            // 2、根据账户和加密密码查询数据库
            Emp emp = empDao.selectEmpByLoginAccount(loginAccount, encodedLoginPassword);
    
            // 3、检查 Emp 对象是否为 null
            if (emp != null) {
                //	①不为 null:返回 Emp
                return emp;
            } else {
                //	②为 null:抛登录失败异常
                throw new LoginFailedException(ImperialCourtConst.LOGIN_FAILED_MESSAGE);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    ⑦EmpDao 方法

    images

    public class EmpDaoImpl extends BaseDao<Emp> implements EmpDao {
        @Override
        public Emp selectEmpByLoginAccount(String loginAccount, String encodedLoginPassword) {
    
            // 1、编写 SQL 语句
            String sql = "select emp_id empId," +
                    "emp_name empName," +
                    "emp_position empPosition," +
                    "login_account loginAccount," +
                    "login_password loginPassword " +
                    "from t_emp where login_account=? and login_password=?";
    
            // 2、调用父类方法查询单个对象
            return super.getSingleBean(sql, Emp.class, loginAccount, encodedLoginPassword);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    ⑧临时页面

    images

    DOCTYPE html>
    <html lang="en" xml:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>临时title>
    head>
    <body>
    
        <p th:text="${session.loginInfo}">p>
    
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    3、退出登录

    ①在临时页面编写超链接

    images

    <a th:href="@{/auth?method=logout}">退朝a>
    
    • 1

    ②在 AuthServlet 编写退出逻辑

    images

    protected void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        // 1、通过 request 对象获取 HttpSession 对象
        HttpSession session = request.getSession();
    
        // 2、将 HttpSession 对象强制失效
        session.invalidate();
    
        // 3、回到首页
        String templateName = "index";
        processTemplate(templateName, request, response);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
  • 相关阅读:
    Android之自定义时间选择弹框
    CSDN每日一题学习训练——Java版(对给定的两个日期之间的日期进行遍历、子集 II、填充每个节点的下一个右侧节点指针)
    桌面平台层安全随手记录
    Django基本使用
    GitOps 实践之渐进式发布
    《树莓派4B家庭服务器搭建指南》第二十一期:安装开源远程桌面服务rustdesk, 内网丝滑,外网流畅控制
    微服务架构设计:构建高可用性和弹性的应用
    Python-数据结构-集合
    用HTML+CSS+JS做一个漂亮简单的公司网站(JavaScript期末大作业)
    小学生python游戏编程7----角色精灵定义
  • 原文地址:https://blog.csdn.net/m0_52896752/article/details/126256456