【问题标题】:Oauth 1.0a consumer code equesting an access token twiceOauth 1.0a 消费者代码两次请求访问令牌
【发布时间】:2016-07-13 16:20:30
【问题描述】:

我已经设置了一个消费者应用程序,并且大多数 oauth 工作流程看起来都是正确的,但是由于某种原因,在提供者调用回调 url 之后,它会尝试两次获取访问令牌。第一次成功

http://localhost:8080/app/ws/oauth/token [OAuth的oauth_consumer_key = “itd79n64zlwv5hhv”,oauth_nonce = “cac26978-c36c-4a8b-8f3e-3e779ff927ab”,oauth_signature = “5c8BM9qQoijXC2f5IXpQGtSQsys%3D”,oauth_signature_method = “HMAC-SHA1”,oauth_timestamp = “1458938403”,组oauth_token =“5451cf20-7eed -4797-819c-ee2316981654", oauth_verifier="c56de555-79df-455e-ab87-f5f11b953fef", oauth_version="1.0"]

响应为 200,payload 包括 oauth_token=a95d6305-4261-4c1d-a9b0-43411a0c2f2c&oauth_token_secret=573702d2-70ca-412c-84e5-868e9ee07169

然后,它再次调用该 URL。

http://localhost:8080/app/ws/oauth/token [OAuth的oauth_consumer_key = “itd79n64zlwv5hhv”,oauth_nonce = “6c013ef9-2f3c-49dd-84fb-97db73b5fb39”,oauth_signature = “5RTQE5XtcqUwEFVvYQjExhH1eio%3D”,oauth_signature_method = “HMAC-SHA1”,oauth_timestamp = “1458938403”,组oauth_token =“5451cf20-7eed -4797-819c-ee2316981654", oauth_verifier="c56de555-79df-455e-ab87-f5f11b953fef", oauth_version="1.0"

由于请求令牌已被删除且访问令牌已发出,因此导致服务器出现异常。

单步执行代码时,我可以看到 OAuthConsumerContextFilter 在第一次调用后很好地存储了访问令牌。

过滤器链以某种方式结束,并使用请求令牌将其带回 CoreOAuthConsumerSupport 中的 readResource。

我使用 spring-boot 构建了消费者应用程序。

来自:applicationContext.xml

    <bean id="oscarService" class="com.mdumontier.oscar.labline.service.OscarService">
        <property name="oscarRestTemplate">
            <bean class="org.springframework.security.oauth.consumer.client.OAuthRestTemplate">
                <constructor-arg ref="oscar" />
            </bean>
        </property>

    </bean>

 <security:authentication-manager>
    <security:authentication-provider>
      <security:user-service>
        <security:user name="marissa" password="wombat" authorities="ROLE_USER" />
        <security:user name="sam" password="kangaroo" authorities="ROLE_USER" />
      </security:user-service>
    </security:authentication-provider>
  </security:authentication-manager>

<security:http auto-config='true' >

  </security:http>



 <oauth:consumer resource-details-service-ref="resourceDetails" oauth-failure-page="/oauth_error.jsp">
    <oauth:url pattern="/oscar/**" resources="oscar"/>
  </oauth:consumer>


  <oauth:resource-details-service id="resourceDetails">
    <oauth:resource id="oscar"
                    key="itd79n64zlwv5hhv"
                    secret="d3psvmrn8k1xws9x"
                    request-token-url="http://localhost:8080/app/ws/oauth/initiate"
                    user-authorization-url="http://localhost:8080/app/ws/oauth/authorize"
                    access-token-url="http://localhost:8080/app/ws/oauth/token"/>
  </oauth:resource-details-service>

【问题讨论】:

    标签: oauth spring-security


    【解决方案1】:

    Spring Boot 会自动在主应用过滤器链中注册任何实现过滤器的 Bean。请参阅:https://stackoverflow.com/a/28428154 了解更多详情。

    oauth:consumer 帮助器将两个 OAuth 过滤器注册为 bean,并且似乎有一段时间没有更新。我什至无法让 XML 配置在最新的 Spring Boot 下正常工作。无论如何,这意味着两者都将运行两次,并且在 OAuthConsumerContextFilter 的情况下,这是破坏性的,因为它将在安全子链之外运行并且每次都失败。

    要解决此问题,您有两个选择。

    第一,提示 Spring Boot 通过为每个自动拾取的过滤器提供一个 FilterRegistrationBean 来避免这种行为,如下所示:

    @Bean
    public FilterRegistrationBean registration(OAuthConsumerContextFilter filter) {
        FilterRegistrationBean registration = new FilterRegistrationBean(filter);
        registration.setEnabled(false);
        return registration;
    }
    

    二,完全绕过 XML 配置并使用 Java 配置。我已在此答案中发布了使用 Java 配置在 Spring Boot 中获取 OAuth 1 使用者的完整工作代码示例:https://stackoverflow.com/a/42143001/2848158

    在 Java 配置中,您将不得不重复 FilterRegistrationBean 技巧,或者只是不首先将这些过滤器注册为 bean,而是直接使用安全过滤器链创建和注册实例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-02
      • 2011-09-19
      • 1970-01-01
      • 2010-11-26
      • 2015-09-25
      • 2012-03-27
      • 1970-01-01
      • 2014-11-15
      相关资源
      最近更新 更多