3行代码快速实现Spring Boot Oauth2服务功能

2025-05-29 0 37

这里的3行代码并不是指真的只需要写3行代码,而是基于我已经写好的一个spring boot oauth2服务。仅仅需要修改3行数据库配置信息,即可得到一个spring boot oauth2服务。

项目地址https://github.com/jeesun/oauthserver

oauthserver

简介

oauthserver是一个基于spring boot oauth2的完整的独立的oauth服务器。仅仅需要创建相关数据表,修改数据库的连接信息,你就可以得到一个oauth服务器。

支持的关系型数据库:

  • postgresql
  • mysql

已实现的功能:

  1. 集成spring boot oauth2,实现oauth服务;
  2. token保存到关系型数据库;
  3. 日志记录保存到文件,并按日归档;
  4. 数据库连接信息加密;
  5. 集成druid数据库连接池。

使用流程

1. 建表

postgresql

请执行src/main/resources/schema-pg.sql,完成数据表的创建和测试数据的导入。

mysql

请执行src/main/resources/schema-mysql.sql,完成数据表的创建和测试数据的导入。

2. 修改数据库连接信息

在application.yml中,配置着数据库的连接信息。其中,配置项username和password是要经过jasypt加密的,不能直接填明文。加密密钥由jasypt.encryptor.password配置。你需要使用test目录下的utiltests工具得到加密字符串。

1

2

3

4

5

6

7

8

9

10

11

12
postgresql

# postgresql连接信息

driver-class-name: org.postgresql.driver

url: jdbc:postgresql://127.0.0.1:5432/thymelteuseunicode=true&characterencoding=utf-8

username: enc(htpbg9fq+7p3sntmxuntdxbtwdqrupv+)

password: enc(abdq6lyospryfqhcqzemtxrozyjvjia4)

mysql

# mysql连接信息

driver-class-name: com.mysql.jdbc.driver

url: jdbc:mysql://127.0.0.1:3306/testuseunicode=true&characterencoding=utf-8&usessl=false

username: enc(yiyjvwtuldgn//yab3kbua==)

password: enc(9oaijkfggsdfahh3oxy63rhwq+amdmij)

3. 运行

现在,一切已准备就绪。运行项目,当程序成功启动时,即表明你已配置成功。

4. 测试

在建表时,我已经向表添加了测试数据。以下请求参数的值,均是测试数据,在数据表中可以找得到。请根据需求到数据表中修改对应的值。

在表oauth_client_details表中,已有一条测试数据。列client_id和client_secret的值,分别对应basic oauth的请求参数username和password的值。而列access_token_validity和列refresh_token_validity,分别代表access_token和refresh_token的有效期时间,以秒为单位。测试数据7200和5184000,分别代表2个小时和2个月(60天)。这是一个比较合理的有效期时间的设置,可以参考。

token相关的接口,都需要进行basic oauth认证。

1、根据用户名和密码获取access_token

posthttp://localhost:8182/oauth/tokengrant_type=password&username=jeesun&password=1234567890c

成功示例:

1

2

3

4

5

6

7
{

"access_token": "ca582cd1-be6c-4a5a-82ec-10af7a8e06eb",

"token_type": "bearer",

"refresh_token": "c24a6143-97c8-4642-88b9-d5c5b902b487",

"expires_in": 3824,

"scope": "read write trust"

}

失败示例(用户名或者密码错误)

1

2

3

4
{

"error": "invalid_grant",

"error_description": "bad credentials"

}

2、检查access_token

gethttp://localhost:8182/oauth/check_tokentoken=ca582cd1-be6c-4a5a-82ec-10af7a8e06eb

成功示例

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17
{

"aud": [

"oauth2-resource"

],

"exp": 1524507296,

"user_name": "jeesun",

"authorities": [

"role_admin",

"role_user"

],

"client_id": "clientidpassword",

"scope": [

"read",

"write",

"trust"

]

}

失败示例(access_token已过期)

1

2

3

4
{

"error": "invalid_token",

"error_description": "token was not recognised"

}

3、根据refresh_token获取新的access_token

posthttp://localhost:8182/oauth/tokengrant_type=refresh_token&refresh_token=c24a6143-97c8-4642-88b9-d5c5b902b487

成功示例

1

2

3

4

5

6

7
{

"access_token": "690ecd7d-f2b7-4faa-ac45-5b7a319478e8",

"token_type": "bearer",

"refresh_token": "c24a6143-97c8-4642-88b9-d5c5b902b487",

"expires_in": 7199,

"scope": "read write trust"

}

app实践指南

app获取到token信息后,需要保存token信息和请求时间。在传access_token之前,需要检查access_token是否过期。为了减少后台压力,检查access_token是否过期应该是在app本地完成。通过token的keyexpires_in(剩余有效期)的值,以及本地记录的请求时间,和当前时间做对比,可以很方便地判断出access_token是否过期。如果过期了,需要通过refresh_token获取新的access_token。因为access_token的有效期只有2个小时,这个验证是必须的。refresh_token同理。

总结

以上所述是小编给大家介绍的3行代码快速实现spring boot oauth2服务,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!

原文链接:https://www.jianshu.com/p/20488e032771

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 3行代码快速实现Spring Boot Oauth2服务功能 https://www.kuaiidc.com/111761.html

相关文章

发表评论
暂无评论