五,搭建环境:辅助功能
文章目录
- 五,搭建环境:辅助功能
- 编写登录失败异常
- 编写常量类
- MD5 工具 (加密工具类)
- 日志配置文件
编写登录失败异常
我们在 demo-module04-util
模块下,创建一个名为:com.rainbowsea.imperial.court.exception
的包下,创建一个名为:LoginFailedException 的异常类——> 作为登录异常的处理。
package com.rainbowsea.imperial.court.exception;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);}
}
编写常量类
我们在 demo-module04-util
模块下,创建一个名为:com.rainbowsea.imperial.court.utils
的包下,创建一个名为:ImperialCourtConst类——> 作为常量类,定义我们项目中所需要的一些常量信息。
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类——> 作为工具类,定义我们项目中所需要的一些工具方法。
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
不可以是其他的,必须是这个 。
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true"><!-- 指定日志输出的位置 --><appender name="STDOUT"class="ch.qos.logback.core.ConsoleAppender"><encoder><!-- 日志输出的格式 --><!-- 按照顺序分别是:时间、日志级别、线程名称、打印日志的类、日志主体内容、换行 --><pattern>[%d{HH:mm:ss.SSS}] [%-5level] [%thread] [%logger] [%msg]%n</pattern><charset>UTF-8</charset></encoder></appender><!-- 设置全局日志级别。日志级别按顺序分别是:DEBUG、INFO、WARN、ERROR --><!-- 指定任何一个日志级别都只打印当前级别和后面级别的日志。 --><root level="INFO"><!-- 指定打印日志的appender,这里通过“STDOUT”引用了前面配置的appender --><appender-ref ref="STDOUT" /></root><!-- 专门给某一个包指定日志级别 --><logger name="com.atguigu" level="DEBUG" additivity="false"><appender-ref ref="STDOUT" /></logger></configuration>
对应顺序上一节内容:✏️✏️✏️ 四,搭建环境:表述层-CSDN博客
对应顺序是下一节内容:✏️✏️✏️ 六,业务功能:登录-CSDN博客