第五节 搭建环境:辅助功能
1、登录失败异常
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e8adf72b06566e3cd36a623debb2081d.png#pic_center)
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);
}
}
2、常量类
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4ddf33d2fd968f82d0d5aa6d68a02044.png#pic_center)
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";
}
3、MD5 工具
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b906801574c3a0f4b42bad703cd803cb.png#pic_center)
public class MD5Util {
public static String encode(String source) {
if (source == null || "".equals(source)) {
throw new RuntimeException("用于加密的明文不可为空");
}
String algorithm = "md5";
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance(algorithm);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
byte[] input = source.getBytes();
byte[] output = messageDigest.digest(input);
int signum = 1;
BigInteger bigInteger = new BigInteger(signum, output);
int radix = 16;
String encoded = bigInteger.toString(radix).toUpperCase();
return encoded;
}
}
4、日志配置文件
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7867ad41f3af2808631fb2c16c78d6da.png#pic_center)
<?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>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
<logger name="com.atguigu" level="DEBUG" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
</configuration>
第六节 业务功能:登录
1、AuthController
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/249f7103002c123f969e6c1c862f9003.png#pic_center)
@Controller
public class AuthController {
@Autowired
private EmpService empService;
@RequestMapping("/auth/login")
public String doLogin(
@RequestParam("loginAccount") String loginAccount,
@RequestParam("loginPassword") String loginPassword,
HttpSession session,
Model model
) {
Emp emp = empService.getEmpByLogin(loginAccount, loginPassword);
if (emp == null) {
model.addAttribute("message", ImperialCourtConst.LOGIN_FAILED_MESSAGE);
return "index";
} else {
session.setAttribute("loginInfo", emp);
return "target";
}
}
}
2、EmpService
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d824e84567d989b8c72a0c638e72b95b.png#pic_center)
@Service
@Transactional(readOnly = true)
public class EmpServiceImpl implements EmpService {
@Autowired
private EmpMapper empMapper;
@Override
public Emp getEmpByLogin(String loginAccount, String loginPassword) {
String encodedLoginPassword = MD5Util.encode(loginPassword);
EmpExample example = new EmpExample();
EmpExample.Criteria criteria = example.createCriteria();
criteria.andLoginAccountEqualTo(loginAccount).andLoginPasswordEqualTo(encodedLoginPassword);
List<Emp> empList = empMapper.selectByExample(example);
if (empList != null && empList.size() > 0) {
return empList.get(0);
}
return null;
}
}
3、target.html
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/65259bcbad06f23cb30a47cbe83746de.png#pic_center)
<!DOCTYPE html>
<html lang="en" xml:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p th:text="${session.loginInfo}"></p>
</body>
</html>