【问题标题】:OAuth2 client fails when redirecting from the authorization step从授权步骤重定向时,OAuth2 客户端失败
【发布时间】:2020-07-16 01:01:33
【问题描述】:

在我当前的 spring-boot 中,我正在尝试使用外部 OAuth2 服务器登录用户。现在的问题是,当我执行应用程序时,授权成功后,用户应该被重定向回应用程序。发生这种情况时,我得到了一个错误。

我的application.properties 文件:

spring.security.oauth2.client.registration.mercadolivre.provider=mercadolivre
spring.security.oauth2.client.registration.mercadolivre.client-id=...
spring.security.oauth2.client.registration.mercadolivre.client-secret=...
spring.security.oauth2.client.registration.mercadolivre.authorization-grant-type=authorization_code
spring.security.oauth2.client.registration.mercadolivre.redirect-uri=http://localhost:8080/
spring.security.oauth2.client.provider.mercadolivre.authorization-uri=https://auth.mercadolivre.com.br/authorization
spring.security.oauth2.client.provider.mercadolivre.token-uri=https://api.mercadolibre.com/oauth/token

我的安全配置类:

@Configuration
public class Security extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .anyRequest().authenticated()
            .and()
            .oauth2Login();
    }
}

我现在面临的错误:

这里有什么问题?

更新

我尝试将此行添加到我的applicatio.properties 文件中:

spring.security.oauth2.client.registration.mercadolivre.redirect-uri={baseUrl}/login/oauth2/code/{registrationId}

现在我收到此错误:

打开浏览器开发者控制台:

【问题讨论】:

    标签: spring-boot spring-security spring-oauth2


    【解决方案1】:

    将redirect-uri 更改为其他内容...最好使用reference 使用的模板字符串:

    spring.security.oauth2.client.registration.mercadolivre.redirect-uri={baseUrl}/login/oauth2/code/{registrationId}
    

    这是处理授权服务器发回的授权代码的 uri。如果您希望用户在成功验证后被重定向到某个页面,请在 configure() 中执行类似的操作:

    http.
        ...
        .oauth2Login()
            .defaultSuccessUrl("/");
    

    【讨论】:

    • 我需要在我的控制器中映射这个redirect-uri,并对收到的数据做一些处理?
    • 不! Spring Security 在 OAuth2LoginAuthenticationFilter 中自动为您执行此操作
    • 好的,我试过了,但现在我收到了这个错误:[invalid_token_response] An error occurred while attempting to retrieve the OAuth 2.0 Access Token Response: 406 Not Acceptable: [Media is not supported]。
    • 你确定你有正确的令牌 uri 吗?您提供的端点没有发回带有“application/json”媒体类型的响应
    • 我使用的正是这里显示的网址:developers.mercadolivre.com.br/en_us/…
    猜你喜欢
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    • 2019-02-01
    • 2015-01-11
    相关资源
    最近更新 更多