Spring Security快速入门
基础环境搭建
创建Spring Boot项目,引入Web、Thymeleaf依赖
引入页面资源文件
在项目resources下的templates目录下,index.html文件是项目首页页面,detail文件夹下的common和vip文件夹分别对应的是普通用户和VIP用户可访问的页面
index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
<meta charset="UTF-8">
<title>影视直播厅</title>
</head>
<body>
<h1 align="center">欢迎进入电影网站首页</h1>
<hr>
<h3>普通电影</h3>
<ul>
<li><a th:href="@{/detail/common/1}">飞驰人生</a> </li>
<li><a th:href="@{/detail/common/2}">夏洛特烦恼</a> </li>
</ul>
<h3>VIP专享</h3>
<ul>
<li><a th:href="@{/detail/vip/1}">速度与激情</a> </li>
<li><a th:href="@{/detail/vip/2}">猩球崛起</a> </li>
</ul>
</body>
</html>
common下的1.html
属性th:href="@{/}",用于配置返回项目首页链接
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>影片详情</title>
</head>
<body>
<a th:href="@{/}">返回</a>
<h1>飞驰人生</h1>
<p style="width: 550px">简介:曾经在赛车界叱咤风云、如今却只能经营炒饭大排档的赛车手张驰(沈腾饰)决定重返车坛挑战年轻一代的天才。
然而没钱没车没队友,甚至驾照都得重新考,这场笑料百出不断被打脸的复出之路,还有更多哭笑不得的窘境在等待着这位过气车神……
</p>
</body>
</html>
common下的2.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>影片详情</title>
</head>
<body>
<a th:href="@{/}">返回</a>
<h1>夏洛特烦恼</h1>
<p style="width: 550px">简介:《夏洛特烦恼》是开心麻花2012年首度推出的话剧,由闫非和彭大魔联合编剧、执导。
2013、2014、2015年仍在上演。该作讲述了一个普通人在穿越重回到高中校园并实现了种种梦想的不可思议的经历……</p>
</body>
</html>
vip下的1.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>影片详情</title>
</head>
<body>
<a th:href="@{/}">返回</a>
<h1>速度与激情</h1>
<p style="width: 550px">简介:《速度与激情》是罗伯·科恩等执导,于2001年至2017年范·迪塞尔、保罗·沃克(已故)、
乔丹娜·布鲁斯特、米歇尔·罗德里格兹等主演的赛车题材的动作犯罪类电影,截至2018年,一共拍了八部。之后两部续集正式定档,
《速度与激情9》和《速度与激情10》分别于2020年4月10日和2021年4月2日上映……</p>
</body>
</html>
vip下的2.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>影片详情</title>
</head>
<body>
<a th:href="@{/}">返回</a>
<h1>猩球崛起</h1>
<p style="width: 550px">简介:《猩球崛起》是科幻片《人猿星球》的前传,由鲁伯特·瓦耶特执导,詹姆斯·弗兰科,汤姆·费尔顿,
芙蕾达·平托,布莱恩·考克斯等主演。剧情主要讲述人猿进化为高级智慧生物、进而攻占地球之前的种种际遇,主题是带有警世性质的——人类疯狂的野心所产生的恶果。
影片获得了第39届安妮奖最佳真人电影角色动画奖等奖项,同时获得了奥斯卡最佳特效奖提名,但很遗憾,并没有获奖……</p>
</body>
</html>
编写控制层
新建controller包,用于页面请求处理的控制类FileController
@Controller
public class FileController {
//影片详情页
@GetMapping("/detail/{type}/{path}")
public String toDetail(@PathVariable("type")String type, @PathVariable("path")String path){
return "detail/"+type+"/"+path;
}
}
到这就完成了使用Spring Boot整合SpringMVC框架实现了一个传统简单的web项目
开启安全管理效果测试
Spring Boot项目中开启Spring Security的方式非常简单,只需添加spring-boot-starter-security启动器
<!-- Spring Security安全框架启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
项目启动成功,都会在控制台随机生成一个安全密码,用户名默认:user,浏览器访问“http://localhost:8080/"查看首页
结果自行查看
自定义用户认证
数据准备
建一个数据库springbootdata的数据库,创建3各表t_customer,t_authorty和t_customer_authority
# 选择使用数据库
USE springbootdata;
# 创建表t_customer并插入相关数据
DROP TABLE IF EXISTS `t_customer`;
CREATE TABLE `t_customer` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`username` varchar(200) DEFAULT NULL,
`password` varchar(200) DEFAULT NULL,
`valid` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO `t_customer` VALUES ('1', 'shitou', '$2a$10$5ooQI8dir8jv0/gCa1Six.GpzAdIPf6pMqdminZ/3ijYzivCyPlfK', '1');
INSERT INTO `t_customer` VALUES ('2', '李四', '$2a$10$5ooQI8dir8jv0/gCa1Six.GpzAdIPf6pMqdminZ/3ijYzivCyPlfK', '1');
# 创建表t_authority并插入相关数据
DROP TABLE IF EXISTS `t_authority`;
CREATE TABLE `t_authority` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`authority` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO `t_authority` VALUES ('1', 'ROLE_common');
INSERT INTO `t_authority` VALUES ('2', 'ROLE_vip');
# 创建表t_customer_authority并插入相关数据
DROP TABLE IF EXISTS `t_customer_aut