最近在学习oauth2,搭建demo工程,password认证模式正常,但authorization_code模式,获取到code后,通过code换取token时,一直报异常
{
"error": "invalid_grant",
"error_description": "Redirect URI mismatch."
}
反复检查参数,都没发现异常
最终通过debug,才发现,原来参数名写错了,将 redirect_url 改为 redirect_uri, 调用正常。
附录 oauth2 调用栈
step1:
package org.springframework.security.oauth2.provider.endpoint.TokenEndpoint
->public ResponseEntity<OAuth2AccessToken> postAccessToken(Principal principal, @RequestParam Map<String, String> parameters)
OAuth2AccessToken token = this.getTokenGranter().grant(tokenRequest.getGrantType(), tokenRequest);
step2:
org.springframework.security.oauth2.provider.CompositeTokenGranter
->public OAuth2AccessToken grant(String grantType, TokenRequest tokenRequest)
grant = granter.grant(grantType, tokenRequest);
step3:
org.springframework.security.oauth2.provider.token.AbstractTokenGranter
->public OAuth2AccessToken grant(String grantType, TokenRequest tokenRequest)
return this.getAccessToken(client, tokenRequest);
step4:
org.springframework.security.oauth2.provider.token.AbstractTokenGranter
->protected OAuth2AccessToken getAccessToken(ClientDetails client, TokenRequest tokenRequest)
return this.tokenServices.createAccessToken(this.getOAuth2Authentication(client, tokenRequest));
step5:
org.springframework.security.oauth2.provider.code.AuthorizationCodeTokenGranter
->protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, TokenRequest tokenRequest)
String redirectUri = (String)parameters.get("redirect_uri");