SpringBoot整合SpringSecurity
配置环境
配置idea
我使用的是idea,点击New Project
点击next项目信息配置随意,再下一步选上下图所示的组件
配置thymeleaf
在pom.xml中加入
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
在application.yml中加入
spring:
thymeleaf:
suffix: .html
cache: false
//设置为传统模式,防止因为严格的语法检测遇到的各种麻烦,例如<html />后习惯不会去加斜杠就会被当做错误检测
mode: LEGACYHTML5
配置传统检测模式需要额外导入上述的dependency并配合配置文件
配置JPA
在application.yml中加入
spring:
datasource:
url: jdbc:mysql://localhost:3306/springsecurity
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
jpa:
show-sql: true
//由于jpa默认将驼峰命名的entity转化为带下划线的名称去匹配数据库中的表名,而我在数据库中也是使用驼峰命名,所以需要下入下列的配置
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
配置数据库
导入如下代码
DROP TABLE IF EXISTS `Sys_permission`;
CREATE TABLE `Sys_permission` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(200) DEFAULT NULL,
`description` varchar(200) DEFAULT NULL,
`url` varchar(200) DEFAULT NULL,
`pid` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
LOCK TABLES `Sys_permission` WRITE;
/*!40000 ALTER TABLE `Sys_permission` DISABLE KEYS */;
INSERT INTO `Sys_permission` (`id`, `name`, `description`, `url`, `pid`)
VALUES
(1,'ROLE_HOME','index','/',NULL),
(2,'ROLE_ADMIN','admin','/admin',NULL),
(3,'ROLE_USER','user','/user',NULL);
/*!40000 ALTER TABLE `Sys_permission` ENABLE KEYS */;
UNLOCK TABLES;
# Dump of table Sys_permission_role
# ------------------------------------------------------------
DROP TABLE IF EXISTS `Sys_permission_role`;
CREATE TABLE `Sys_permission_role` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`role_id` int(11) unsigned NOT NULL,
`permission_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `role2` (`role_id`),
KEY `permission` (`permission_id`),
CONSTRAINT `permission` FOREIGN KEY (`permission_id`) REFERENCES `Sys_permission` (`id`),
CONSTRAINT `role2` FOREIGN KEY (`role_id`) REFERENCES `Sys_Role` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
LOCK TABLES `Sys_permission_role` WRITE;
/*!40000 ALTER TABLE `Sys_permission_role` DISABLE KEYS */;
INSERT INTO `Sys_permission_role` (`id`, `role_id`, `permission_id`)
VALUES
(10,2,1),
(11,2,3),
(12,3,1),
(13,3,2),
(15,2,2);
/*!40000 ALTER TABLE `Sys_permission_role` ENABLE KEYS */;
UNLOCK TABLES;
# Dump of table Sys_Role
# ------------------------------------------------------------
DROP TABLE IF EXISTS `Sys_Role`;
CREATE TABLE `Sys_Role` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(200) DEFAULT NULL,
PRIMARY KEY (