OAuth2RestTemplate

时间:2016-08-10 21:44:29

标签: proxy spring-security-oauth2 oauth2 oauth2client

我需要使用受OAuth2保护的API。为此我使用OAuth2RestTemplate。 但是我得到了错误:

java.net.ConnectException: Connection timed out: connect

由于代理问题,这种情况正在发生。我知道如何在RestTemplate中设置代理:

 SimpleClientHttpRequestFactory clientHttpRequestFactory = new       SimpleClientHttpRequestFactory();
 Proxy proxy = new Proxy(Proxy.Type.HTTP, new      InetSocketAddress("Proxy host", 8080));

clientHttpRequestFactory.setProxy(代理);       RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);

我尝试设置 OAuth2RestTemplate 的方式相同:

@Bean
public OAuth2RestOperations restTemplate(OAuth2ClientContext oauth2ClientContext) {
    OAuth2RestTemplate client =  new OAuth2RestTemplate(resource(), oauth2ClientContext);
    SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_HOST, PROXY_PORT));
    clientHttpRequestFactory.setProxy(proxy);
    client.setRequestFactory(clientHttpRequestFactory);
    return client;
}

但它不起作用并且给予"连接超时"例外。发生这种情况的原因是第一行OAuth2RestTemplate client = new OAuth2RestTemplate(resource(), oauth2ClientContext);试图获取Access令牌,这意味着它还需要代理设置。如果我添加以下行,那么它可以工作:

System.setProperty("https.proxyHost", "urproxy.com");
System.setProperty("https.proxyPort", "8080");

但我无法使用System.setProperties("","")选项,因为我们没有权限在tomcat服务器上设置。

我研究过但在创建此对象时找不到任何在OAuth2RestTemplate中设置代理的方法。

任何帮助将不胜感激。感谢

2 个答案:

答案 0 :(得分:9)

OAuth2RestTemplate只创建一组AccessTokenProvider,以根据不同类型的授权类型从授权服务器检索令牌。例如,AuthorizationCodeAccessTokenProvider用于检索具有grant type authorization_code的访问令牌。令牌提供者自己发起一些RestTemplate来发送请求但不使用刚刚创建的OAuth2RestTemplate。一种方法可以解决问题。那就是创建自己的AccessTokenProvider并设置请求工厂。

SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
Proxy proxy= new Proxy(Type.HTTP, new InetSocketAddress(PROXY_HOST, PROXY_PORT));
requestFactory.setProxy(proxy);      

AuthorizationCodeAccessTokenProvider authorizationCodeAccessTokenProvider = new AuthorizationCodeAccessTokenProvider();
authorizationCodeAccessTokenProvider.setRequestFactory(requestFactory);

ImplicitAccessTokenProvider implicitAccessTokenProvider = new ImplicitAccessTokenProvider();
implicitAccessTokenProvider.setRequestFactory(requestFactory);

AccessTokenProvider accessTokenProvider = new AccessTokenProviderChain(
Arrays.<AccessTokenProvider> asList(authorizationCodeAccessTokenProvider, implicitAccessTokenProvider));

OAuth2RestTemplate client = new OAuth2RestTemplate(github(), oauth2ClientContext);
client.setAccessTokenProvider(accessTokenProvider);

您还可以将ResourceOwnerPasswordAccessTokenProvider和ClientCredentialsAccessTokenProvider添加到OAuth2RestTemplate。

答案 1 :(得分:0)

此RestTemplate提供了一种解决方法:

/**
 * An OAuth2RestTemplate with proxy support.
 * 
 * @author E.K. de Lang
 */
public class ProxySupportingOAuth2RestTemplate
    extends OAuth2RestTemplate
{
    private static final Logger LOG = LogFactory.getLogger(ProxySupportingOAuth2RestTemplate.class);

    private final SimpleClientHttpRequestFactory factory;

    public ProxySupportingOAuth2RestTemplate(OAuth2ProtectedResourceDetails resource, OAuth2ClientContext context,
            AccessTokenProvider accessTokenProvider)
    {
        super(resource, context);
        factory = new SimpleClientHttpRequestFactory();
        super.setRequestFactory(factory);
        super.setAccessTokenProvider(accessTokenProvider);

        // To fix issue: https://github.com/spring-projects/spring-security-oauth/issues/459 also set the factory of the token-provider.
        if (accessTokenProvider instanceof OAuth2AccessTokenSupport) {
            ((OAuth2AccessTokenSupport) accessTokenProvider).setRequestFactory(factory);
        }
        else {
            throw new UnsupportedOperationException("accessTokenProvider must extend OAuth2AccessTokenSupport");
        }
    }

    public void setProxy(Proxy proxy)
    {
        if (LOG.isDebugEnabled()) {
            LOG.debug("setProxy:" + proxy);
        }
        if (super.getRequestFactory() == factory) {
            factory.setProxy(proxy);
        }
        else {
            throw new UnsupportedOperationException("RequestFactory has changed.");
        }
    }
}