在Spring Cloud需要使用oauth2来实现多个微服务的统一认证授权,通过向OAUTH服务发送某个类型的grant type进行集中认证和授权,从而获得access_token,而这个token是受其他微服务信任的,我们在后续的访问可以通过access_token来进行,从而实现了微服务的统一认证授权。
客户端根据约定的ClientID、ClientSecret、Scope来从Access Token URL地址获取AccessToken,并经过AuthURL认证,用得到的AccessToken来访问其他资源接口。
Spring Cloud oauth2 需要依赖Spring security
1、获取授权码,回调返回code(code有效期10分钟,使用过后必须重新获取),如下图:
http://localhost:8115/oauth/authorize?response_type=code&client_id=client_1&redirect_uri=http://www.baidu.com
2、授权码模式:通过code,获取accessToken,如下图:
http://localhost:8115/oauth/token?grant_type=authorization_code&code=wOWRd9&redirect_uri=http://www.baidu.com&scope=all&client_id=client_1&client_secret=123456
3、密码模式:通过用户名和密码,获取accessToken,如下图:
http://localhost:8115/oauth/token?grant_type=password&redirect_uri=http://www.mayikt.com&scope=all&username=user_1&password=123456&client_id=client_1&client_secret=123456
http://localhost:8115/oauth/token?grant_type=refresh_token&refresh_token=02521fd5-5233-4c80-b12a-231bef1c1a14&client_id=client_1&client_secret=123456
http://localhost:8115/oauth/check_token?token=2eee1e81-d14c-4d24-91f6-42d4d3c4a348