说明
(1)JDK版本:1.8
(2)Spring Boot 2.0.6
(3)Spring Security 5.0.9
(4)Spring Data JPA 2.0.11.RELEASE
(5)hibernate5.2.17.Final
(6)MySQLDriver 5.1.47
(7)MySQL 8.0.12
需求缘起
在前面使用的是内存数据库,本节使用MySQL数据库进行操作。
编码思路
在《基于内存数据库的身份认证和角色授权》的代码转换为MySQL数据库的存储方式的话,非常的简单,只需要添加MySQL数据库的驱动包以及配置好数据源和JPA。
一、基于MySQL数据库的身份认证和角色授权
1.1 添加依赖
在pom.xml文件中去掉hsqldb的依赖,然后添加mysql的依赖:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
1.2 添加配置信息
在application.properties文件中添加配置信息:
spring.datasource.url = jdbc:mysql://localhost:3306/spring-security
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl aut