OAuth2RestTemplate 提供的访问令牌已过期

OAuth2RestTemplate The access token provided has expired

我尝试了很长时间的解决方案,但没有找到任何有用的东西。 现在,直接进入问题。

我必须使用远程 OAuth2 身份验证服务器并且我有使用它的凭据。身份验证用于 REST 服务调用。 REST 服务每 15 分钟调用一次,我们的应用程序在一次迭代中调用多个 URL。

第一次调用 REST API,一切正常,我们获得了访问令牌,这很有用。 1 小时后,当访问令牌过期并再次调用 REST API 时,我们收到以下错误:

The access token provided has expired

此错误有时显示一次,有时显示两次或更多次。 再过 15 分钟后,REST API 调用再次无误。

我尝试使用 属性 retryBadAccessTokens,但没有成功。 我们正在使用具有以下设置的 spring-security-oauth2-2.0.8.RELEASE

<bean id="oipRestTemplate" class="org.springframework.security.oauth2.client.OAuth2RestTemplate" scope="prototype">
    <constructor-arg ref="oipClientCredentialsResourceDetail"/>
    <property name="accessTokenProvider" ref="tokenProvider"/>
    <property name="requestFactory" ref="httpComponentsClientHttpRequestFactory"/>
    <property name="retryBadAccessTokens" value="true"/>
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" >
                <property name="supportedMediaTypes">
                    <list>
                        <bean class="org.springframework.http.MediaType">
                            <constructor-arg value="application" />
                            <constructor-arg value="json" />
                        </bean>
                    </list>
                </property>
            </bean>
        </list>
    </property>
    <property name="errorHandler">
        <bean class="our.company.app.holders.MyResponseErrorHandler" />
    </property>
</bean>

看看他们是否也给你一个刷新令牌。您使用它们来代表用户生成令牌。

顺便说一句,如果你每 15 分钟打一次电话,我每次都会得到新的令牌。当您每分钟拨打数千个电话时,您不想这样做……但是 15 分钟?没什么大不了的。

我们有一个相当高的流量系统,我注意到我们遇到的失败总是在令牌即将过期的那一刻。 有没有可能是client看token,快过期了,发给server,现在过期了,server拒绝了

如果是这种情况,我们将需要一些缓冲区 space,其中至少包含 Oauth2RestTemplate 查看其令牌副本与服务器查看令牌之间的时间差。

我正在考虑将 mod 发布到 Oauth2RestTemplate 本身。

if (accessToken == null || accessToken.isExpired())

应该

if (accessToken == null || accessToken.isAboutToExpire())

有一些关于多早申报的配置值。