1. Preface
In the previous article we found the filter OAuth2AuthorizationRequestRedirectFilter that intercepts the OAuth2 authorization request entry /oauth2/authorization and found the method that actually initiates the OAuth2 authorization request sendRedirectForAuthorization. But this method is not described in detail, so I’ll continue today.
2. sendRedirectForAuthorization
This sendRedirectForAuthorization method is not much code, its main purpose is to redirect access to third-party platforms for authorization. All its logic is related to OAuth2AuthorizationRequest, so we can’t gloss over OAuth2AuthorizationRequest, we have to understand how OAuth2AuthorizationRequest came to be and what it’s for.
OAuth2AuthorizationRequestResolver
This requires analyzing the parser class OAuth2AuthorizationRequestResolver, whose core methods have two overloads, one of which is sufficient here.
|
|
The resolve(request, registrationId, redirectUriAction) method inside the above method is the fundamental method to finally extract the OAuth2AuthorizationRequest from /oauth2/authorization. There’s a lot of code but I’ll try to diagram it in a way that’s easy to understand. The resolve method assembles different OAuth2AuthorizationRequests depending on the authorization type (AuthorizationGrantType).
3. OAuth2AuthorizationRequest
The next is the core of the OAuth2.0 protocol is the most important, you may later customize the reference from here, this time circle up to test the knowledge points. I will OAuth2AuthorizationRequestResolver in a variety of ways to authorize the resolution of the OAuth2AuthorizationRequest object for a complete summary of induction. It is roughly divided into the following two parts.
3.1 Determined by AuthorizationGrantType
The combing of OAuth2AuthorizationRequest under different AuthorizationGrantType. The member variables involved are.
authorizationGrantType, from the configurationspring.security.client.registration.{registrationId}.authorizationGrantType.responseType, determined by the value ofauthorizationGrantType, refer to the JSON below.additionalParameters, some additional parameters are required when the value ofauthorizationGrantTypeisauthorization_code, refer to the JSON below.attributes, different attributes exist for differentauthorizationGrantType.
where a form like
{registrationId}means that{registrationId}is a variable, e.g.registrationId=gitee.
There are five cases in the OAuth2 client configuration spring.security.client.registration.{registrationId} prefix.
When scope does not contain openid and client-authentication-method is not none the above four parameters.
The above four parameters when scope contains openid and client-authentication-method is not none.
The above four parameters when scope does not contain openid and client-authentication-method is none.
|
|
The above four parameters when scope contains openid and client-authentication-method is none.
|
|
It is much simpler under implicit.
3.2 Fixed rules section
The above is the personalized value strategy for the member variables of OAuth2AuthorizationRequest under various AuthorizationGrantType, and there are several parameters whose rules are fixed
clientIdcomes from the configuration and is a unique identifier given to us by the third-party platform.authorizationUricomes from the configuration and is used to construct the request URL to the third party.scopesfrom the configuration, is the scope of the authorization given to us by the third-party platform, which can be understood as roles.stateis generated automatically, to prevent csrf attacks.authorizationRequestUriis the authorization request to the third-party platform, which can be set directly by theOAuth2AuthorizationRequestbuilder class or generated by theauthorizationUriparameters above, and the construction mechanism will be analyzed later.redirectUriWhenOAuth2AuthorizationRequestis received by the third-party platform, the third-party platform will call back this URI to respond to the authorization request, and the mechanism will be analyzed later.
The construction mechanism of authorizationRequestUri
If authorizationRequestUri is not provided explicitly, it will be constructed using the
responseTypeclientIdscopesstateredirectUriadditionalParametersare stitched into theauthorizationUriparameter string according to the following rules, with thekeyandvalueof the parameter string being URI-encoded.
|
|
Then the OAuth2AuthorizationRequestRedirectFilter is responsible for redirecting to the authorizationRequestUri to request authorization from the third party.
redirectUri
The third party will call redirectUri when it receives the response, and the callback has certain rules, following the path parameter rule {baseUrl}/{action}/oauth2/code/{registrationId}.
baseUrlis the base request path extracted from our/oauth2/authorizationrequest.action, which has two default valueslogin,authorize, and will be populated based on the value ofactionwhen the/oauth2/authorizationrequest contains theactionparameter.registrationIdThis goes without saying.
4. Summary
By analyzing the rules of the OAuth2AuthorizationRequest request object in detail, we should be able to roughly know the flow of the filter OAuth2AuthorizationRequestRedirectFilter.
- build
ClientRegistrationthrough the client configuration, which can subsequently be persisted. - intercept
/oauth2/authorizationrequest and constructOAuth2AuthorizationRequest, then redirect toauthorizationRequestUrito request authorization. - the third party is redirected to
redirect_urifor the corresponding request.
So how does Spring Security OAuth2 handle third-party callbacks accordingly? This part will be covered in the next article.
Reference https://felord.cn/oauth2-authorization-request.html