提问者:小点点

Angular-如何使用OAuth2令牌进一步超文本传输协议Rest Api调用springboot


成功的Oaut2(google auth)后,角应用程序在重定向url中接收从sprinfboot服务器生成的令牌。如何使用此令牌进一步调用springboot的rest api?


共2个答案

匿名用户

如何创建新的安全请求对Oaut2服务器在这里找到:https://www.oauth.com/oauth2-servers/making-authenticated-requests/

对于你的问题,这部分很重要:

POST /resource/1/update HTTP/1.1
Authorization: Bearer RsT5OjbzRn430zqMLgV3Ia"
Host: api.authorization-server.com
 

简而言之,您必须使用您收到的密钥“承载者”令牌在请求标头中添加授权参数。

匿名用户

显然,您正在使用授权码流程获取令牌。在此流程中,首先客户端/应用程序从授权服务器请求auth code。服务器返回为应用程序提供/配置的重定向uri中的代码。然后客户端通过再次请求授权服务器将此auth codeaccess_token交换。

授权码请求通常有以下格式

https://<authorization_server>/oauth/authorize
?client_id=<registered_client id with authorization server>
&response_type=code
&state=<nonce>
&redirect_uri=<redirection endpoint to receive the code>

现在,您的应用程序需要将前一阶段提取的auth code交换为access_token来发出API请求。

请求通常如下所示

POST /oauth/token HTTP/1.1
Host: <authroization server>

client_id=<registered_client id with authorization server>
&client_secret=<client seret obtained while registration>
&grant_type=code
&code=<code return in the previous request>
&redirect_uri=<redirection endpoint mentioned in the previous request>

代码交换请求的成功响应如下。

{
    "token_type": "bearer",
    "access_token": "JDrM5knpv5YG0wTWnQbIqM75zdL80qmREsP2ucL4",
    "refresh_token": "v4YG0wTWnQbIqMoDrM5knp0qmREsP2ucL475zdL8",
    "expires_in": 3600
}

现在,您可以在标题中传递之前获得的access_token,以便向Resouce server发出请求。

Authorization: Bearer JDrM5knpv5YG0wTWnQbIqM75zdL80qmREsP2ucL4