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

本文详细介绍了如何基于Spring Security OAuth2实现OAuth2的数据库版,包括创建oauth2数据库、初始化表结构、用户权限管理及数据库配置。通过示例代码和步骤,展示了从内存版过渡到数据库版的整个过程,涉及了UserDetailsService、JdbcClientDetailsService和jdbcTokenStore的使用。

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

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

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

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

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

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

2728086218e6176ac7d34d66376242e0.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 tb_user ( id

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值