OAUTH2四种模式

2022-07-23  本文已影响0人  良先生爱生活

写在前边

客户端:你的网站
认证服务器/服务端:微信

1、授权码模式:authorization code(微信网页登录)

1)用户访问客户端,客户端(网站)将用户导向认证服务器
2)用户选择是否给客户端授权

response_type=code
scope
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect

3)假设用户给予授权,认证服务器将用户302到客户端(网站)指定的URI上,并同时给出一个授权码 code

redirect_url?code=xx

4)客户端(网站)收到授权码,向认证服务器发送code和URI(URL目前没有见过),认证服务器核对code后,返回access_token 和 refresh_token

grant_type=authorization_code 授权码模式
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code

详见:微信网页授权文档

2、客户端模式(企业和企业的B2B交互模式与C端用户无关,基本都是分销对接之类在使用)

1)客户端(网站)向认证服务器通过client_id和secert获取Header的Authorization basic

grant_type=client_credentials
https://www.authorize.com/Authorize/token
POST
client_id:xxx
secret:xxx
grant_type:client_credentials

2)认证服务器返回一个token,在请求其它服务端API时在Header带上,有scope限制

看业务
Authorization:bear xxxxxxtokenxxxxxxx
或
Authorization:basic xxxxxxtokenxxxxxxx

3、密码模式(简单)

1)用户访问客户端(网站)时需要输入用户名和密码,客户端(网站)再向认证服务器去获取access_token

https://www.authorize.com?response_type=password&username=xxx&password=xxx

认证服务器返回access_token

千万不要对第三方企业提供账号密码,只能用在自家的服务器中使用,防止泄密 !!!

4、简化模式 Implicit Grant Type (非安全模式)不推荐且没用过

1)用户访问客户端(网站),客户端(网站)将用户导向认证服务器
2)用户选择是否给客户端(网站)授权

response_type=token
http://www.authorize.com?response_type=token&client_id=xxx&state=xxx&redirect_url=xxx

3)用户授权后认证服务器将用户导向 redirect_url中且包含了access_token

redirect_url?access_token=xxx
上一篇 下一篇

猜你喜欢

热点阅读