【问题标题】:Spring boot with oAuth2 OauthException: “You must provide a client_id”带有 oAuth2 OauthException 的 Spring Boot:“您必须提供 client_id”
【发布时间】:2016-08-29 00:20:04
【问题描述】:

在我的项目中,我正在尝试使用 instagram 对用户进行身份验证。 该项目在 spring-boot 上使用 spring-security-oauth2 运行。 在身份验证阶段,我从 instagram code 收到,它被发送到 instagram 以检索 access_token。而不是access_token,我收到了错误的请求响应消息:“您必须提供client_id”。

WebSecurityConfig.class

 @Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .antMatcher("/**")
            .authorizeRequests()
            .antMatchers("/", "/index**", "/login/**", "/webjars/**", "/js/**", "/css/**", "/images/**")
            .permitAll()
            .anyRequest()
            .authenticated()
            .and().exceptionHandling().authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/"))
            .and().logout().logoutSuccessUrl("/").permitAll()
            .and().addFilterAfter(csrfHeaderFilter(), CsrfFilter.class)
            .addFilterBefore(ssoFilter(instagram(), "/login"), BasicAuthenticationFilter.class);
}
@Bean
@ConfigurationProperties("instagram")
ClientResources instagram() {
    return new ClientResources();
}

private Filter ssoFilter(ClientResources client, String path) {
    OAuth2ClientAuthenticationProcessingFilter filter = new OAuth2ClientAuthenticationProcessingFilter(path) {

    };
    OAuth2RestTemplate filterTemplate = new OAuth2RestTemplate(client.getClient(), oauth2ClientContext);
    filter.setRestTemplate(filterTemplate);
    filter.setTokenServices(new UserInfoTokenServices(client.getResource().getUserInfoUri(),
                                                              client.getClient().getClientId()));
    return filter;
}

application.properties

instagram.client.clientId=df5cc67d71e04b1b9a89ca9e1572a801
instagram.client.clientSecret=8da04f4072a54579ae4e334d5e865fa4
instagram.client.accessTokenUri=https://api.instagram.com/oauth/access_token
instagram.client.userAuthorizationUri=https://api.instagram.com/oauth/authorize
instagram.client.scope=basic, likes, comments, relationships
instagram.resource.userInfoUri=https://api.instagram.com/v1/users/self

Here 是 github 上的那个项目。用于教育目的的项目,所以这里开放了clientId和clientSecret。 感谢您提供有关如何提前解决该烦人异常的建议

【问题讨论】:

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


    【解决方案1】:

    在 OAuth2 中有多个流程,我们可以在其中执行身份验证授权。您选择的流程是授权码流程。

    在授权码流程中,授权服务器将授权码返回给用户代理(浏览器)。然后浏览器将该代码发布回资源服务器(在服务器上运行的 Api 代码)。然后服务器将该代码与客户端 ID 和客户端密码一起交换为 access_token。

    客户端 ID 和密码是机密的,因此当您在基于 Javascript 的应用程序中使用 OAuth2 时,建议使用隐式流。在此流程中,授权服务器使用 access_token 而不是授权码进行响应。

    如果这有帮助或您有任何其他问题,请告诉我。

    谢谢你, 索玛。

    【讨论】:

    • 我在这里没有看到任何有用的信息。使用了显式代码流,但它不起作用。
    • 我没有看到带有 client_id、client_secret 和访问代码的 access_token 调用
    • 因为 spring-oauth 正在为我做这一切。问题出在身份验证的最后一步。我明白了,你不明白我的问题,你的回答只是从一些 oauth 文档中复制粘贴。
    • K.我误解了你的问题。我没有复制我的答案,它是基于错误消息“您必须提供 client_id”。我会查看您的 github 解决方案,看看是否有帮助。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    • 2019-08-28
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多