麻花痒 发表于 2024-8-9 09:11:51

五,搭建环境:辅助功能

五,搭建环境:辅助功能

@
目录

[*]五,搭建环境:辅助功能
[*]编写登录失败异常
[*]编写常量类
[*]MD5 工具 (加密工具类)
[*]日志配置文件

编写登录失败异常

我们在 demo-module04-util 模块下,创建一个名为:com.rainbowsea.imperial.court.exception的包下,创建一个名为:LoginFailedException 的异常类——> 作为登录异常的处理。
https://img2024.cnblogs.com/blog/3084824/202408/3084824-20240809101202996-520560296.png
package com.rainbowsea.imperial.court.exception;

public class LoginFailedExceptionextends 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);
    }
}编写常量类

我们在 demo-module04-util 模块下,创建一个名为:com.rainbowsea.imperial.court.utils的包下,创建一个名为:ImperialCourtConst类——> 作为常量类,定义我们项目中所须要的一些常量信息。
https://img2024.cnblogs.com/blog/3084824/202408/3084824-20240809101202897-1606840524.png
package com.rainbowsea.imperial.court.utils;

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";
}MD5 工具 (加密工具类)

我们在 demo-module04-util 模块下,创建一个名为:com.rainbowsea.imperial.court.utils的包下,创建一个名为:MD5Util类——> 作为工具类,定义我们项目中所须要的一些工具方法。
https://img2024.cnblogs.com/blog/3084824/202408/3084824-20240809101202993-453127310.png
package com.rainbowsea.imperial.court.utils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

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;
    }
}日志配置文件

在该 demo-module01-web 模块下的src/main/resources根路径下,创建一个名为logback.xml 的日志配置文件。注意:文件名就是为logback.xml不可以是其他的,必须是这个 。
https://img2024.cnblogs.com/blog/3084824/202408/3084824-20240809101202899-1686649794.png
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
   
    <appender name="STDOUT"
            >
      <encoder>
            
            
            <pattern>[%d{HH:mm:ss.SSS}] [%-5level] [%thread] [%logger] [%msg]%n</pattern>
            <charset>UTF-8</charset>
      </encoder>
    </appender>

   
   
    <root level="INFO">
      
      <appender-ref ref="STDOUT" />
    </root>

   
    <logger name="com.atguigu" level="DEBUG" additivity="false">
      <appender-ref ref="STDOUT" />
    </logger>

</configuration>

[*]对应顺序上一节内容:✏️✏️✏️    四,搭建环境:表述层-CSDN博客
[*]对应顺序是下一节内容:✏️✏️✏️

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 五,搭建环境:辅助功能