该过滤器是Spring Security的内置过滤器,它负责将最终用户(End User)发起的请求/oauth2/authorization/gitee重定向到授权服务器的授权端点(authorization uri)来启动授权码授权流程。它的结构如下:

这里比较重要的是OAuth2AuthorizationRequestResolver接口。
此接口的实现能够从提供的HttpServletRequest提取OAuth2授权端点authorization-uri所需的参数,并封装为OAuth2AuthorizationRequest对象。 如果你涉及到一些自定义类型的授权请求,特别是自定义参数,就可以自定义该接口。
OAuth2AuthorizationRequest接口默认的实现是DefaultOAuth2AuthorizationRequestResolver。两个方法的逻辑很相似,这里仅仅分析下面这个方法:


当 scope 不包含openid而且client-authentication-method不为none时上述四个参数:
{
"authorizationGrantType": "authorization_code",
"responseType": "code",
"additionalParameters": {},
"attributes": {
"registration_id": "{registrationId}"
}
}
当 scope 包含openid而且client-authentication-method不为none时上述四个参数:
{
"authorizationGrantType": "authorization_code",
"responseType": "code",
"additionalParameters": {
"nonce": "{nonce}的Hash值"
},
"attributes": {
"registration_id": "{registrationId}",
"nonce": "{nonce}"
}
}
当 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}"
}
}
当 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}"
}
}
在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参数串}
组装完毕由OAuth2AuthorizationRequestRedirectFilter重定向到authorizationRequestUri向第三方请求授权。
{baseUrl}/{action}/oauth2/code/{registrationId}
OAuth2AuthorizationRequest大概率需要定制,为此DefaultOAuth2AuthorizationRequestResolver提供了一个Consumer类型的函数来满足OAuth2AuthorizationRequest定制化的需求。
除此之外OAuth2AuthorizationRequest.Builder也提供了两个定制函数:
parametersConsumer 来定制authorizationRequestUri所需要的参数。authorizationRequestUriFunction 来定制最终的URI。