【SpringBoot 搭建个人博客】- 后台登录(四)
之前提到过,由于是个人博客,就没有做权限管理,只是简单的区分了一下管理员(栈主)和普通用户,所以这里需要用户实体类,并需要基本的用户名和密码,管理员登录后可以对后台进行操作,而普通用户则没有权限,在 com.star(Group 组名)目录下创建 entity 包,并创建 User 实体类,实体类如下(这里省去了 get、set、toString 方法):
package com.star.entity;
import java.util.Date;
/**
@Description: 用户实体类
@Date: Created in 21:39 2020/5/26
@Author: ONESTAR
@QQ 群: 530311074
@URL: https://onestar.newstar.net.cn/
*/
public class User {
private Long id;
private String nickname;
private String username;
private String password;
private String email;
private String avatar;
private Integer type;
private Date createTime;
private Date updateTime;
}
2.MD5 加密
由于后面要用到 MD5 加密,对登录密码进行加密,这里就先进行处理一下,在 com.star 包下创建 util 工具包,用来放工具类,创建 MD5Utils 工具类,如下:
package com.star.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
@Description: MD5 加密工具类
@Date: Created in 17:16 2020/5/27
@Author: ONESTAR
@QQ 群: 530311074
@URL: https://onestar.newstar.net.cn/
*/
public class MD5Utils {
/**
@Description: MD5 加密
@Auther: ONESTAR
@Date: 17:19 2020/5/27
@Param: 要加密的字符串
@Return: 加密后的字符串
*/
public static String code(String str){
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(str.getBytes());
byte[]byteDigest = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < byteDigest.length; offset++) {
i = byteDigest[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
//32 位加密
return buf.toString();
// 16 位的加密
//return buf.toString().substring(8, 24);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
System.out.println(code("111111"));
}
}
分析:
通过该工具类,可以获取密码,在 main 函数中输入自己密码对应的明码,然后运行,可以在控制台获取对应的密码,这个密码是要存储在数据库中的 password 字段
eg:这里是"111111"字符串,运行 main 后,获得密码为:"96e79218965eb72c92a549dd5a330112",则将该字符串存储进数据库中
3.持久层接口
在 com.star 目录下创建 dao 包,创建用户持久层接口 UserDao,这里主要查询用户名和密码,通过 @Param 注解将参数传递给 SQL,代码如下:
package com.star.dao;
import com.star.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
/**
@Description: 用户持久层接口
@Date: Created in 0:06 2020/5/27
@Author: ONESTAR
@QQ 群: 530311074
@URL: https://onestar.newstar.net.cn/
*/
@Mapper
@Repository
public interface UserDao {
/**
@Description:
@Auther: ONESTAR
@Date: 10:24 2020/5/27
@Param: username:用户名;password:密码
@Return: 返回用户对象
*/
User findByUsernameAndPassword(@Param("username") String username, @Param("password") String password);
}
分析:
@Mapper 注解:让 Mybatis 找到对应的 mapper,在编译的时候动态生成代理类,实现相应 SQL 功能
@Repository 注解:用来声明 dao 层的 bean(这个注解可有可无,可以消去依赖注入的报错信息)【@Mapper 和 @Repository 注解可以参考这篇文章:Mybatis 中的 @Repository 与 @Mapper】
@Param 注解:将参数传递给 SQL
返回一个 User 对象给 service 调用并核对用户名和密码
4.mapper
Mybatis 使用 XMLMMapperBuilder 类的实例来解析 mapper 配置文件并执行 SQL 语句,在 resources 目录下创建 mapper 文件夹,再创建 UserDao.xml 文件,如下:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.star.dao.UserDao">
<select id="findByUsernameAndPassword" resultType="com.star.entity.User">
select * from myblog.t_user
where user
name = #{username} and password = #{password};
</select>
</mapper>
5.用户业务层
在 com.star 目录下创建 service 包,创建用户业务层接口 UserService,这里主要是检验用户名和密码,传递用户名和密码两个参数,代码如下:
package com.star.service;
import com.star.entity.User;
/**
@Description: 用户业务层接口
@Date: Created in 22:56 2020/5/26
@Author: ONESTAR
@QQ 群: 530311074
@URL: https://onestar.newstar.net.cn/
*/
public interface UserService {
//核对用户名和密码
User checkUser(String username, String password);
}
用户层接口实现类:
在 service 包下创建 Impl 包,用来放接口实现类,UserServiceImpl 代码如下:
package com.star.service.Impl;
import com.star.dao.UserDao;
import com.star.entity.User;
import com.star.service.UserService;
import com.star.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
@Description: 用户业务层接口实现类
@Date: Created in 23:01 2020/5/26
@Author: ONESTAR
@QQ 群: 530311074
@URL: https://onestar.newstar.net.cn/
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
/**
@Description:
@Auther: ONESTAR
@Date: 21:25 2020/5/27
@Param: username:用户名;password:密码
@Return: 返回用户对象
*/
@Override
public User checkUser(String username, String password) {
User user = userDao.findByUsernameAndPassword(username, MD5Utils.code(password));
return user;
}
}
分析:
这里主要是获取数据库中的用户名和密码,通过控制器传递过来的密码进行解析匹配,匹配成功则登录
6.登录控制器
在 controller 控制器包下创建 admin 包,用来放用户管理的控制器,创建 LoginController 用户登录控制器,在这里进行登录跳转、登录校验、注销功能,代码如下:
package com.star.controller.admin;
import com.star.entity.User;
import com.star.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpSession;
/**
@Description: 用户登录控制器
@Date: Created in 21:40 2020/5/27
@Author: ONESTAR
@QQ 群: 530311074
@URL: https://onestar.newstar.net.cn/
*/
@Controller
@RequestMapping("/admin")
public class LoginController {
@Autowired
private UserService userService;
/**
@Description: 跳转登录页面
@Auther: ONESTAR
@Date: 21:57 2020/5/27
@Param:
@Return: 返回登录页面
*/
@GetMapping
public String loginPage(){
return "admin/login";
}
/**
@Description: 登录校验
@Auther: ONESTAR
@Date: 10:04 2020/5/27
@Param: username:用户名
@Param: password:密码
@Param: session:session 域
@Param: attributes:返回页面消息
@Return: 登录成功跳转登录成功页面,登录失败返回登录页面
*/
@PostMapping("/login")
public String login(@RequestParam String username,
@RequestParam String password,
HttpSession session,
RedirectAttributes attributes) {
User user = userService.checkUser(username, password);
if (user != null) {
user.setPassword(null);
session.setAttribute("user",user);
return "admin/index";
} else {
attributes.addFlashAttribute("message", "用户名和密码错误");
return "redirect:/admin";
}
}
/**
@Description: 注销
@Auther: ONESTAR
@Date: 10:15 2020/5/27
@Param: session:session 域
@Return: 返回登录页面
*/
@GetMapping("/logout")
public String logout(HttpSession session) {
session.removeAttribute("user");
return "redirect:/admin";
}
}
分析:
评论