sprint oauth2 mysql_基于spring-security-oauth2实现oauth2数据库版(持续更新)

本文档详细介绍了如何使用Spring Security OAuth2在MySQL数据库中实现OAuth2认证服务,包括创建数据库表结构、初始化数据以及相关配置。通过数据库存储客户端、用户和权限信息,实现了RBAC权限角色管理。
摘要由CSDN通过智能技术生成

基于spring-security-oauth2实现oauth2数据库版

文章代码地址:链接描述可以下载直接运行,基于springboot2.1.5,springcloud Greenwich版本实现

该系列分为两个部分:分为内存实现,数据库实现。其中数据库实现采用RBAC权限角色管理。

上一篇,介绍了oauth2的内存实现,也就是认证服务把客户端和用户信息都存储在内存中,这样不利于拓展,不适合于生产环境。下面,我们开始基于mysql数据库的oauth2实现。

首先,我们创建oauth2数据库,注意编码选择utf-8mb4格式,utf-8是不规范的,mysql也没有进行更改。

4623ae2827e9395739d23612990f3dda.png

好了,现在我们初始化表,sql如下:

Drop table if exists oauth_client_details;

create table oauth_client_details (

client_id VARCHAR(255) PRIMARY KEY,

resource_ids VARCHAR(255),

client_secret VARCHAR(255),

scope VARCHAR(255),

authorized_grant_types VARCHAR(255),

web_server_redirect_uri VARCHAR(255),

authorities VARCHAR(255),

access_token_validity INTEGER,

refresh_token_validity INTEGER,

additional_information TEXT,

autoapprove VARCHAR (255) default 'false'

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Drop table if exists oauth_access_token;

create table oauth_access_token (

token_id VARCHAR(255),

token BLOB,

authentication_id VARCHAR(255),

user_name VARCHAR(255),

client_id VARCHAR(255),

authentication BLOB,

refresh_token VARCHAR(255)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Drop table if exists oauth_refresh_token;

create table oauth_refresh_token (

token_id VARCHAR(255),

token BLOB,

authentication BLOB

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Drop table if exists oauth_code;

create table oauth_code (

code VARCHAR(255),

authentication BLOB

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- Add indexes

create index token_id_index on oauth_access_token (token_id);

create index authentication_id_index on oauth_access_token (authentication_id);

create index user_name_index on oauth_access_token (user_name);

create index client_id_index on oauth_access_token (client_id);

create index refresh_token_index on oauth_access_token (refresh_token);

create index token_id_index on oauth_refresh_token (token_id);

create index code_index on oauth_code (code);

-- INSERT DEFAULT DATA

INSERT INTO oauth_client_details VALUES ('dev', '', 'dev', 'app', 'authorization_code', 'http://localhost:7777/', '', '3600', '3600', '{"country":"CN","country_code":"086"}', 'TAIJI');

-- ----------------------------

-- Table structure for tb_user

-- ----------------------------

DROP TABLE IF EXISTS tb_user;

CREATE TABLE

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值