这里的3行代码并不是指真的只需要写3行代码,而是基于我已经写好的一个spring boot oauth2服务。仅仅需要修改3行数据库配置信息,即可得到一个spring boot oauth2服务。
项目地址https://github.com/jeesun/oauthserver
oauthserver
简介
oauthserver是一个基于spring boot oauth2的完整的独立的oauth服务器。仅仅需要创建相关数据表,修改数据库的连接信息,你就可以得到一个oauth服务器。
支持的关系型数据库:
- postgresql
- mysql
已实现的功能:
使用流程
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
相关文章
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
Debian 8或Debian 9(64 位)安装 .NET Core
2025-05-29 110 -
详细探讨台湾数据中心的环境特点、基础设施建设、及其对业务运营的影响
2025-05-25 41 -
2025-05-29 80
-
2025-05-25 29
-
2025-05-27 48

