Spring Boot+Spring Security:基于MySQL数据库的身份认证和角色授权

本文介绍了如何在Spring Boot项目中使用Spring Security进行身份认证和角色授权,通过MySQL数据库存储用户信息。从内存数据库转换到MySQL数据库只需添加依赖和配置数据源,接着展示了如何加密保存数据库密码,以及在遇到连接MySQL8+时的2059错误的解决方法。
摘要由CSDN通过智能技术生成

说明

(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
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值