• 【Oauth2】四、OAuth2AuthorizationRequestRedirectFilter


    一、OAuth2AuthorizationRequestRedirectFilter

    该过滤器是Spring Security的内置过滤器,它负责将最终用户(End User)发起的请求/oauth2/authorization/gitee重定向到授权服务器的授权端点(authorization uri)来启动授权码授权流程。它的结构如下:
    在这里插入图片描述
    这里比较重要的是OAuth2AuthorizationRequestResolver接口。

    二、OAuth2AuthorizationRequestResolver

    此接口的实现能够从提供的HttpServletRequest提取OAuth2授权端点authorization-uri所需的参数,并封装为OAuth2AuthorizationRequest对象。 如果你涉及到一些自定义类型的授权请求,特别是自定义参数,就可以自定义该接口。

    1、DefaultOAuth2AuthorizationRequestResolver

    OAuth2AuthorizationRequest接口默认的实现是DefaultOAuth2AuthorizationRequestResolver。两个方法的逻辑很相似,这里仅仅分析下面这个方法:
    在这里插入图片描述

    • 1、匹配/oauth2/authorization/{registrationId}并提取路径参数获得registrationId。
    • 2、 然后去请求对象request中提取key为action的参数,默认值是login。
    • 3、执行封装的底层resolve方法生成OAuth2AuthorizationRequest
      上面步骤③ 的resolve方法会根据不同的授权方式(AuthorizationGrantType)来组装不同的OAuth2AuthorizationRequest。
      在这里插入图片描述
      上图中黄色的字段是动态组装的,其它字段根据说明组装。黄色组装逻辑可分为以下两种情况。

    2、授权码模式

    当 scope 不包含openid而且client-authentication-method不为none时上述四个参数:

    {
      "authorizationGrantType": "authorization_code",
      "responseType": "code",
      "additionalParameters": {},
      "attributes": {
        "registration_id": "{registrationId}"
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    当 scope 包含openid而且client-authentication-method不为none时上述四个参数:

    {
      "authorizationGrantType": "authorization_code",
      "responseType": "code",
      "additionalParameters": {
        "nonce": "{nonce}的Hash值"
      },
      "attributes": {
        "registration_id": "{registrationId}",
        "nonce": "{nonce}"
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    当 scope不包含openid而且client-authentication-method为none时上述四个参数:

    {
      "authorizationGrantType": "authorization_code",
      "responseType": "code",
      "additionalParameters": {
        "code_challenge": "{codeVerifier}的Hash值",
        // code_challenge_method 当不是SHA256可能没有该key
        "code_challenge_method": "S256(如果是SHA256算法的话)"
      },
      "attributes": {
        "registration_id": "{registrationId}",
        "code_verifier": "Base64生成的安全{codeVerifier}"
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    当 scope包含openid而且client-authentication-method为none时上述四个参数:

    {
      "authorizationGrantType": "authorization_code",
      "responseType": "code",
      "additionalParameters": {
        "code_challenge": "{codeVerifier}的Hash值",
        // code_challenge_method 当不是SHA256可能没有该key
        "code_challenge_method": "S256(如果是SHA256算法的话)",
        "nonce": "{nonce}的Hash值"
      },
      "attributes": {
        "registration_id": "{registrationId}",
        "code_verifier": "Base64生成的安全{codeVerifier}",
        "nonce": "{nonce}"
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    3、authorizationRequestUri的构建机制

    OAuth2AuthorizationRequest的构建过程中,如果不显式提供authorizationRequestUri就会通过OAuth2AuthorizationRequest中的responseType、clientId 、scopes、state 、redirectUri、additionalParameters 按照下面的规则进行拼接成authorizationUri的参数串,参数串的key和value都要进行URI编码。

    authorizationUri?response_type={responseType.getValue()}&client_id={clientId}&scope={scopes元素一个字符间隔}&state={state}&redirect_uri={redirectUri}&{additionalParameter展开进行同样规则的KV参数串}
    
    • 1

    组装完毕由OAuth2AuthorizationRequestRedirectFilter重定向到authorizationRequestUri向第三方请求授权。

    • redirectUri
      redirectUri也是由DefaultOAuth2AuthorizationRequestResolver构建的,遵循规则:
    {baseUrl}/{action}/oauth2/code/{registrationId}
    
    • 1
    • baseUrl 是从我们/oauth2/authorization请求中提取的基础请求路径,比如https://felord.cn。
    • action,有两种默认值login、authorize ,当/oauth2/authorization请求中包含了action参数时会根据action的值进行填充。
    • registrationId 请求注册id。

    4、定制OAuth2AuthorizationRequest

    OAuth2AuthorizationRequest大概率需要定制,为此DefaultOAuth2AuthorizationRequestResolver提供了一个Consumer类型的函数来满足OAuth2AuthorizationRequest定制化的需求。

    除此之外OAuth2AuthorizationRequest.Builder也提供了两个定制函数:

    • parametersConsumer 来定制authorizationRequestUri所需要的参数。
    • authorizationRequestUriFunction 来定制最终的URI。
  • 相关阅读:
    使用三丰云免费主机搭建zerotier网络
    JavaWeb:上传文件
    致敬2023,人工智能(AI)一个技术飞速发展的2023
    充分理清限制与条件+构造二分图+最小割:ARC142E
    git撤回本地的commit或者push到远程的代码
    知识分享系统
    【C++】C++基础知识(一)---基本概念
    Jmeter 分布式压测,你的系统能否承受高负载?
    RecycleView的一些使用
    Docker入门之安装Tomcat
  • 原文地址:https://blog.csdn.net/weixin_43333483/article/details/125901284