官术网_书友最值得收藏!

  • OAuth 2.0 Cookbook
  • Adolfo Eloy Nascimento
  • 360字
  • 2021-07-08 09:35:09

How it works...

As we are using the same configuration annotations for the OAuth2AuthorzationServer and OAuth2ResourceServer classes, all the OAuth 2.0 support is being added the same way as that provided by Spring Security OAuth2. The important difference in configurations here is denoted by the usage of an authenticationManager instance. The authenticationManager has to be present because the Authorization Server has to authenticate the Resource Owner's credential when trying to respond for a required access token by the third-party application.

In addition to the configuration differences, we also have an important difference in the authorization flow by itself. When using this grant type, the user (or the Resource Owner) has to send her credentials, which will be in the power of the client application. There must be a trust relation between the Resource Owner and the client application in most cases when dealing with the client and server that are comprised of the same solution (for example, if you as a Resource Owner are interacting with the official Facebook's client application, which in turn interacts with Facebook's server-side applications).

To see how this grant type works for this recipe, start the application and try sending the following request, which is sending the Resource Owner's credentials as the form parameter's username and password respectively (look for the client authentication which must always be performed by the Authorization Server when requesting an access token through the /oauth/token endpoint):

curl -X POST --user clientapp:123456 http://localhost:8080/oauth/token -H "accept: application/json" -H "content-type: application/x-www-form-urlencoded" -d "grant_type=password&username=adolfo&password=123&scope=read_profile"

After running the previous command, you should see the following JSON response (obviously with a different access token):

{
"access_token": "28405009-b53d-4e52-bfc3-c8889a477675",
"token_type": "bearer",
"expires_in": 43199,
"scope": "read_profile"
}

Note that as we are not defining any access token expiration time; Spring Security OAuth2 defines 43200 seconds by default. Depending on the grant type used, you should consider using a short time access token (for example, when using the Implicit grant type).

Now that you have a valid access token, try to retrieve the user's profile through the following command:

curl -X GET http://localhost:8080/api/profile -H "authorization: Bearer 28405009-b53d-4e52-bfc3-c8889a477675"

After running the previous command, you should see the user's name and email.

主站蜘蛛池模板: 信宜市| 农安县| 怀集县| 土默特左旗| 罗江县| 平昌县| 桃江县| 福海县| 伊吾县| 峨眉山市| 辰溪县| 古田县| 长兴县| 德惠市| 南岸区| 巴彦淖尔市| 榆中县| 沂水县| 孟连| 剑河县| 南部县| 榆树市| 伊川县| 太保市| 亳州市| 阿瓦提县| 平南县| 潞西市| 太仓市| 双流县| 那坡县| 南充市| 宜兰市| 呼玛县| 金华市| 卓资县| 余江县| 昆山市| 闸北区| 上犹县| 平山县|