SpringBoot整合SpringSecurity(附带源码)

本文详细介绍了如何在SpringBoot项目中配置SpringSecurity,包括环境配置、JPA设置、数据库连接、包目录结构、DAO层及Entity关系,以及SpringSecurity的拦截策略、自定义登录验证和权限管理。通过实例展示了如何实现用户登录、权限控制等功能,提供了完整的源码链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置环境

配置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 (`id`)
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值