基础准备
操作系统:Windows
Java开发包:JDK8
项目管理工具:Maven 3.9.2
项目开发工具:IntelliJ IDEA
数据库:MySQL
登录
数据准备:在数据库添加3个用户登录信息表
用户表:
-- ---------------------------- -- Table structure for `t_user` -- ---------------------------- DROP TABLE IF EXISTS `t_user`; CREATE TABLE `t_user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(200) DEFAULT NULL, `password` varchar(200) DEFAULT NULL, `email` varchar(200) DEFAULT NULL, `created` date DEFAULT NULL, `valid` tinyint(1) NOT NULL DEFAULT '1', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of t_user -- ---------------------------- INSERT INTO `t_user` VALUES ('1', 'admin', '$2a$10$5ooQI8dir8jv0/gCa1Six.GpzAdIPf6pMqdminZ/3ijYzivCyPlfK', '2127269781@qq.com', '2018-10-01', '1'); INSERT INTO `t_user` VALUES ('2', '李四', '$2a$10$5ooQI8dir8jv0/gCa1Six.GpzAdIPf6pMqdminZ/3ijYzivCyPlfK', '1768653040@qq.com', '2018-11-13', '1'); INSERT INTO `t_user` VALUES ('3', '东方不败', '$2a$10$5ooQI8dir8jv0/gCa1Six.GpzAdIPf6pMqdminZ/3ijYzivCyPlfK', '13718391550@163.com', '2018-12-18', '1'); INSERT INTO `t_user` VALUES ('4', 'tom', '$2a$10$5ooQI8dir8jv0/gCa1Six.GpzAdIPf6pMqdminZ/3ijYzivCyPlfK', 'asexeees@sohu.com', '2018-12-03', '1');
用户权限表:
-- ---------------------------- -- Table structure for `t_authority` -- ---------------------------- DROP TABLE IF EXISTS `t_authority`; CREATE TABLE `t_authority` ( `id` int(11) NOT NULL AUTO_INCREMENT, `authority` varchar(200) DEFAULT NULL COMMENT '权限', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of t_authority -- ---------------------------- INSERT INTO `t_authority` VALUES ('1', 'ROLE_admin'); INSERT INTO `t_authority` VALUES ('2', 'ROLE_common');
用户权限关联表:
-- ---------------------------- -- Table structure for `t_user_authority` -- ---------------------------- DROP TABLE IF EXISTS `t_user_authority`; CREATE TABLE `t_user_authority` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL COMMENT '关联的用户id', `authority_id` int(11) NOT NULL COMMENT '关联的权限id', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of t_user_authority -- ---------------------------- INSERT INTO `t_user_authority` VALUES ('1', '1', '1'); INSERT INTO `t_user_authority` VALUES ('2', '2', '2'); INSERT INTO `t_user_authority` VALUES ('3', '3', '2'); INSERT INTO `t_user_authority` VALUES ('4', '4', '2');
在全局配置文件哪里添加数据库相关配置JDBC
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.initialSize=20 spring.datasource.minIdle=10 spring.datasource.maxActive=100 spring.datasource.url=jdbc:mysql://localhost:3306/blog_system?serverTimezone=UTC&useSSL=false spring.datasource.username=root spring.datasource.password=root
如果没有引入 Spring Security依赖则需要引入依赖
登录配置页面:LoginController
package com.itheima.web.client; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import javax.servlet.http.HttpServletRequest; import java.util.Map; /** * @author * @date 2023/10/26 0:33 * 用户登录模块 */ @Controller public class LoginController { // 向登录页面跳转 @GetMapping(value = "/login") public String login(HttpServletRequest request, Map map) { // 分别获取请求头和参数url中的原始非拦截的访问路径 String referer = request.getHeader("Referer"); String url = request