关于单点登陆的基本实现:点这里
实现一个基于jdbc的OAuth2认证
本文主要介绍使用jdbc存储token的例子。代码基于上一篇文章做一些修改实现。
源码地址
修改项目依赖
project("sso-auth-server") {
dependencies {
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-security'
compile 'org.springframework.boot:spring-boot-starter-jdbc' // 新添加
compile 'org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.0.0.RELEASE'
runtime 'mysql:mysql-connector-java' // 新添加
}
}
创建OAuth2数据存储相关表
Spring官方给出了基于HSQL建表sql。本文数据库使用mysql,对它做了一些修改。看这里。
- 配置数据库链接
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://127.0.0.1:3306/oauth2test
- 创建数据库表
这里直接使用程序调用sql脚本实现。
public class AuthenticationApplication {
private static final Logger log = LoggerFactory.getLogger(AuthenticationApplication.class);
public static void main(String[] args)