编程知识 cdmana.com

Spring security real combat dry goods: how to build and execute oauth2 authorization request

stay Spring Security Combat dry goods : client OAuth2 We found an intercept in the entry to the authorization request OAuth2 Authorization request entry /oauth2/authorization Filter OAuth2AuthorizationRequestRedirectFilter, And found the real launch OAuth2 How to authorize a request sendRedirectForAuthorization. But this method does not elaborate , So today I'll go on to the last article to fill in this hole .

2. sendRedirectForAuthorization

This sendRedirectForAuthorization Methods don't have much code , Its main function is to authorize redirection access to the third-party platform . All its logic is related to OAuth2AuthorizationRequest of , So we are OAuth2AuthorizationRequest It doesn't work to understate , We have to master OAuth2AuthorizationRequest How did you get it , What is it for .

OAuth2AuthorizationRequestResolver

This requires parsing classes OAuth2AuthorizationRequestResolver, Its core approach has two overloads , It's enough to analyze one here .

@Overridepublic OAuth2AuthorizationRequest resolve(HttpServletRequest request) { // registrationId It's through uri Path parameter /oauth2/authorization/{registrationId} To obtain the  String registrationId = this.resolveRegistrationId(request); //  And then ask for the object request Extract from key by action Parameters of , The default value is login String redirectUriAction = getAction(request, "login"); //  And then we go to the fundamental parsing method  return resolve(request, registrationId, redirectUriAction);}

In the above method resolve(request, registrationId, redirectUriAction) It's the method that ultimately comes from /oauth2/authorization extract OAuth2AuthorizationRequest The fundamental method of . There's too much code, but I try to make it easy to understand .resolve Methods will be based on different authorization methods (AuthorizationGrantType) To assemble different OAuth2AuthorizationRequest.

3. OAuth2AuthorizationRequest

The next step is OAuth2.0 At the heart of the agreement , Maybe your customized reference will come from here in the future , It's about knowledge . I will be right. OAuth2AuthorizationRequestResolver Under all kinds of authorization OAuth2AuthorizationRequest Object analysis for a complete summary . It is roughly divided into the following two parts :

3.1 from AuthorizationGrantType Decisive

In different AuthorizationGrantType Next pair OAuth2AuthorizationRequest The carding of . The member variables involved are :

  • authorizationGrantType , From configuration spring.security.client.registration.{registrationId}.authorizationGrantType.
  • responseType , from authorizationGrantType Value determination of , Refer to the following JSON.
  • additionalParameters, When authorizationGrantType The value is authorization_code Some additional parameters are required , Refer to the following JSON .
  • attributes, Different authorizationGrantType There are different attributes .

It's similar to {registrationId} Formal representation of {registrationId} It's a variable , for example registrationId=gitee.

stay OAuth2 Client configuration spring.security.client.registration.{registrationId} There are five situations in the prefix of .

When scope It doesn't contain openid and client-authentication-method Not for none The four parameters mentioned above :

{ "authorizationGrantType": "authorization_code", "responseType": "code", "additionalParameters": {}, "attributes": { "registration_id": "{registrationId}" }}

When scope contain openid and client-authentication-method Not for none The four parameters mentioned above :

{ "authorizationGrantType": "authorization_code", "responseType": "code", "additionalParameters": { "nonce": "{nonce} Of Hash value " }, "attributes": { "registration_id": "{registrationId}", "nonce": "{nonce}" }}

When scope It doesn't contain openid and client-authentication-method by none The four parameters mentioned above :

{ "authorizationGra.........

版权声明
本文为[Irving the procedural ape]所创,转载请带上原文链接,感谢

Scroll to Top