直接进入正题吧,这篇文章直接讲构建完整的kong+oauth2协议的例子。 官方地址oauth插件:https://docs.konghq.com/hub/kong-inc/oauth2/。没有写很完整,只是帮大家方便入门下。
1.创建service
官方service相关接口
这里创建一个名为l-o的服务并配置upstream server。upstream可以配置host, port, protocol, path,也可以直接配置url项将其他一次性配置。service相当于nginx的server。
{{url}}变量为http://localhost:8001,即admin api url
楼主这里的返回值为:
{
"host": "localhost",
"created_at": 1540941923,
"connect_timeout": 60000,
"id": "2354af94-13f5-4e8e-ac44-d52945c53ff1",
"protocol": "http",
"name": "l-o",
"read_timeout": 60000,
"port": 3000,
"path": null,
"updated_at": 1540941923,
"retries": 5,
"write_timeout": 60000
}
2.创建route
官方route相关接口
这里简单创建一个'/', route相当于nginx里的location。
{
"next": null,
"data": [
{
"created_at": 1540942417,
"strip_path": true,
"preserve_host": false,
"regex_priority": 0,
"updated_at": 1540942417,
"paths": [
"/"
],
"service": {
"id": "2354af94-13f5-4e8e-ac44-d52945c53ff1"
},
"protocols": [
"http",
"https"
],
"id": "6251b33a-a08f-434d-a874-f5e51bfcde45"
}
]
}
这是已经可以通过kong访问本地的3000端口了,楼主使用java(方便后续获取用户信息),启动完毕后,访问kong的资源接口。
这里{{uurl}}是kong的资源访问接口http://localhost:8000。Host为service中的host值。
3.为service添加插件
官方plugin相关接口
这里有很多配置项,建议详细看官方文档。scope可以由系统切分模块后配置,例如: read, write。如果我们使用密码授权,一定要设置enable_password_grant为true,因为默认为false
{
"created_at": 1540971779000,
"config": {
"refresh_token_ttl": 1209600,
"scopes": [
"email",
"phone",
"address"
],
"mandatory_scope": true,
"provision_key": "DKQv6RHW9E8QtvXBxjnY9elCAXyLEX1K",
"hide_credentials": false,
"enable_authorization_code": true,
"enable_implicit_grant": false,
"global_credentials": false,
"accept_http_if_already_terminated": false,
"enable_password_grant": true,
"enable_client_credentials": false,
"anonymous": "",
"token_expiration": 7200,
"auth_header_name": "authorization"
},
"id": "0450482d-4b1f-4ae1-81aa-7d629276740e",
"enabled": true,
"service_id": "2354af94-13f5-4e8e-ac44-d52945c53ff1",
"name": "oauth2"
}
这时再次访问资源,会报错
{
"error_description": "The access token is invalid or has expired",
"error": "invalid_token"
}
4.创建consumer
官方consumer相关接口
consumer消费者和upstream service使用用户的概念不一致哦。
{
"custom_id": null,
"created_at": 1540971391,
"username": "goddy-oauth",
"id": "8f2e1aa7-e5e8-4977-94e3-246c521044ec"
}
5.consumer下创建application
相当于创建oauth2中的client,理论上每一个upstream service对应一个consumer
{
"client_id": "oauth2-client",
"created_at": 1540971653000,
"id": "a54a5d85-f903-4db7-9b08-0039825a83a7",
"redirect_uri": [
"http://www.baidu.com"
],
"name": "oauth2-app",
"client_secret": "secret",
"consumer_id": "8f2e1aa7-e5e8-4977-94e3-246c521044ec"
}
6.使用Resource Owner Password Credentials获取token
这里username、password、authenticated_userid,是需要我们自己开发的后端项目(backend)来核对的。
{
"refresh_token": "kmzuQf7ogfFkD2j6v4Fb13X0dRv723MQ",
"token_type": "bearer",
"access_token": "wFNcIS2Jvvqru6uwgUfuBbwV0QUzB8TC",
"expires_in": 7200
}
这时用获取到的token又可以访问资源啦 Authorization里配置Bearer Token
7.使用Authorization Code获取token
{
"redirect_uri": "http://www.baidu.com?code=4yYKuC7oW53GEOqSOqpMzeXvqVeCJsTv"
}
{
"refresh_token": "V6lRoL6QFz0y4wcp6Eng6ziPbCoR0jRz",
"token_type": "bearer",
"access_token": "9eukFVdSHvDSmmG4gMjKu1IfYBY4sU5x",
"expires_in": 7200
}
授权码模式 Authorization Code
=> 1,2,3,4,5,7
密码模式 Resource Owner Password Credentials
=> 1,2,3,4,5,6
网友评论