springboot 整合 oauth2报错 Redirect URI mismatch

最近在学习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");

 

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Spring Boot整合OAuth2可以实现用户认证和授权功能。OAuth2是一种授权框架,常用于保护API端点和限制对用户数据的访问。 下面是一个简单的示例演示了如何在Spring Boot中整合OAuth2: 1. 添加Spring Security和OAuth2依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-client</artifactId> </dependency> ``` 2. 创建一个配置类用于配置OAuth2: ```java @Configuration @EnableWebSecurity public class OAuth2Config extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/").permitAll() .anyRequest().authenticated() .and() .oauth2Login(); } } ``` 在上面的配置中,`configure()`方法配置了HTTP请求的权限规则,`.oauth2Login()`方法启用了OAuth2登录功能。 3. 添加OAuth2客户端配置到application.properties文件: ```properties spring.security.oauth2.client.registration.google.client-id=your-client-id spring.security.oauth2.client.registration.google.client-secret=your-client-secret spring.security.oauth2.client.registration.google.redirect-uri={baseUrl}/{action}/oauth2/code/{registrationId} spring.security.oauth2.client.provider.google.authorization-uri=https://accounts.google.com/o/oauth2/auth spring.security.oauth2.client.provider.google.token-uri=https://accounts.google.com/o/oauth2/token spring.security.oauth2.client.provider.google.user-info-uri=https://www.googleapis.com/oauth2/v3/userinfo spring.security.oauth2.client.provider.google.user-name-attribute=email ``` 上述配置使用了Google作为OAuth2的提供者,你需要替换成自己的客户端ID和客户端密钥。 4. 创建一个控制器用于处理登录成功后的回调: ```java @Controller public class OAuth2LoginController { @GetMapping("/oauth2/login/success") public String loginSuccess() { return "redirect:/"; } } ``` 在上述控制器中,`loginSuccess()`方法处理登录成功后的回调,并重定向到首页。 这只是一个简单的示例,你可以根据自己的需求进行更多的配置和定制化。希望对你有帮助!如果还有其他问题,请继续提问。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值