spring 使用代理创建OAuth2RestTemplate资源

uttx8gqw  于 2023-03-07  发布在  Spring
关注(0)|答案(1)|浏览(162)

我正在尝试创建一个使用OAuth2的客户端,但还需要遍历代理。目前我收到错误Failed to instantiate [org.springframework.web.client.RestTemplate]: Factory method 'restTemplate' threw exception; nested exception is java.lang.NullPointerException我不确定是如何得到的,因为我在方法上使用@Bean注解来确保IoC容器知道,这是不正确的吗?请参阅下面用于此目的的两个方法:

public RestTemplate restTemplate() throws JSONException {
        AccessTokenRequest atr = new DefaultAccessTokenRequest();
        RestTemplate restTemplate = new OAuth2RestTemplate(restTemplateResource(),
                new DefaultOAuth2ClientContext(atr));

        //Proxy Settings
        LOG.error("--in getProxySettings-");
        LOG.info(System.getenv("VCAP_SERVICES"));
        JSONObject vcapServices = new JSONObject(System.getenv("VCAP_SERVICES"));
        LOG.info("vcapServices {}",vcapServices.toString());
        JSONObject cproxy = vcapServices.getJSONArray("c-proxy").getJSONObject(0);
        LOG.info("cproxy {}",cproxy.toString());
        JSONObject credentials = cproxy.getJSONObject("credentials");
        LOG.info("credentials {}",credentials.toString());

        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
                new AuthScope(credentials.getString("host"), credentials.getInt("port")),
                new UsernamePasswordCredentials(credentials.getString("username"), credentials.getString("password"))
        );

        HttpHost myProxy = new HttpHost(credentials.getString("host"), credentials.getInt("port"));
        HttpClientBuilder clientBuilder = HttpClientBuilder.create();

        clientBuilder.setProxy(myProxy).setDefaultCredentialsProvider(credsProvider).disableCookieManagement();

        HttpClient httpClient = clientBuilder.build();
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        factory.setHttpClient(httpClient);

        restTemplate.setRequestFactory(factory);
        return restTemplate;
    }


    protected OAuth2ProtectedResourceDetails restTemplateResource() {

        ClientCredentialsResourceDetails clientCredentialsResourceDetails = new ClientCredentialsResourceDetails();
        clientCredentialsResourceDetails.setAccessTokenUri(websecSecurityDetails.getEndpoint());
        clientCredentialsResourceDetails.setClientId(websecSecurityDetails.getClientId());
        clientCredentialsResourceDetails.setClientSecret(websecSecurityDetails.getClientSecret());
        clientCredentialsResourceDetails.setGrantType(websecSecurityDetails.getGrantType());
        List<String> scopeList = new ArrayList<>();
        scopeList.add(websecSecurityDetails.getScope());
        clientCredentialsResourceDetails.setScope(scopeList);
        clientCredentialsResourceDetails.setAuthenticationScheme(AuthenticationScheme.header);
        return clientCredentialsResourceDetails;
    }
vyswwuz2

vyswwuz21#

这是因为应用程序未部署在env中,因此“VCAP_SERVICES”java操作不可用。

相关问题