18、使用MySQL存储access_token和client信息-之建表语句

 

如果使用jdbc数据库存储client和token信息,那么必须自己手动建立oauth相关的表并写入(注册)数据,否则认证过程中报错:

 

-- used in tests that use HSQL

create table oauth_client_details (

client_id VARCHAR(256) PRIMARY KEY,

resource_ids VARCHAR(256),

client_secret VARCHAR(256),

scope VARCHAR(256),

authorized_grant_types VARCHAR(256),

web_server_redirect_uri VARCHAR(256),

authorities VARCHAR(256),

access_token_validity INTEGER,

refresh_token_validity INTEGER,

additional_information VARCHAR(4096),

autoapprove VARCHAR(256)

);

 

create table oauth_client_token (

token_id VARCHAR(256),

token LONGVARBINARY,

authentication_id VARCHAR(256) PRIMARY KEY,

user_name VARCHAR(256),

client_id VARCHAR(256)

);

 

create table oauth_access_token (

token_id VARCHAR(256),

token LONGVARBINARY,

authentication_id VARCHAR(256) PRIMARY KEY,

user_name VARCHAR(256),

client_id VARCHAR(256),

authentication LONGVARBINARY,

refresh_token VARCHAR(256)

);

 

create table oauth_refresh_token (

token_id VARCHAR(256),

token LONGVARBINARY,

authentication LONGVARBINARY

);

 

create table oauth_code (

code VARCHAR(256), authentication LONGVARBINARY

);

 

create table oauth_approvals (

userId VARCHAR(256),

clientId VARCHAR(256),

scope VARCHAR(256),

status VARCHAR(10),

expiresAt TIMESTAMP,

lastModifiedAt TIMESTAMP

);

 

 

-- customized oauth_client_details table

 

create table ClientDetails (

appId VARCHAR(256) PRIMARY KEY,

resourceIds VARCHAR(256),

appSecret VARCHAR(256),

scope VARCHAR(256),

grantTypes VARCHAR(256),

redirectUrl VARCHAR(256),

authorities VARCHAR(256),

access_token_validity INTEGER,

refresh_token_validity INTEGER,

additionalInformation VARCHAR(4096),

autoApproveScopes VARCHAR(256)

);

 

注意:

    对于MySQL来说,默认建表语句中主键是varchar(255)类型,在mysql中执行会报错,原因是mysql对varchar主键长度有限制。所以这里改成128即可。其次,语句中会有某些字段为LONGVARBINARY类型,它对应mysql的blob类型,也需要修改一下

 

建立表完成后,然后插入语句:

# client_id, resource_ids, client_secret, scope, authorized_grant_types, web_server_redirect_uri, authorities, access_token_validity, refresh_token_validity, additional_information, autoapprove

'client', NULL, 'secret', 'app', 'authorization_code', 'http://www.baidu.com', NULL, NULL, NULL, NULL, NULL

 

这时就可以访问授权页面了:

localhost:8080/oauth/authorize?client_id=client&response_type=code&redirect_uri=http://www.baidu.com

 

授权服务是基于Spring Security的,因此需要在项目中引入两个依赖:

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-security</artifactId>

</dependency>

 

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-oauth2</artifactId>

</dependency>

前者为 Security,后者为Security的OAuth2扩展。

 

在启动类中添加@EnableAuthorizationServer注解

@SpringBootApplication

@EnableAuthorizationServer

public class AlanOAuthApplication {

public static void main(String[] args) {

SpringApplication.run(AlanOAuthApplication.class, args);

}

}

 

授权流程

访问授权页面:

localhost:8080/oauth/authorize?client_id=client&response_type=code&redirect_uri=http://www.baidu.com

 

此时浏览器会让你输入用户名密码,这是因为 Spring Security 在默认情况下会对所有URL添加Basic Auth认证。默认的用户名为user, 密码是随机生成的,在控制台日志中可以看到。

 

画风虽然很简陋,但是基本功能都具备了。点击Authorize后,浏览器就会重定向到百度,并带上code参数:

 

 

拿到code以后,就可以来换取access_token了

POST/GET http://client:secret@localhost:8080/oauth/token

 

curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'grant_type=authorization_code&code=Li4NZo&redirect_uri=http://www.baidu.com' "http://client:secret@localhost:8080/oauth/token"

 

注意:URL中的client为上文中通过ClientDetailsServiceConfigurer类指定的clientId。由于authorization_code的授权方式不需要 client_secret, 因此secret可以填写任意值

 

快来成为我的朋友或合作伙伴,一起交流,一起进步!
QQ群:961179337
微信:lixiang6153
邮箱:lixx2048@163.com
公众号:IT技术快餐
更多资料等你来拿!

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贝壳里的沙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值