2017-03-06 14 views
0

REST APIにアクセスするときに自分自身を認証する必要があります。apache camelルートにwindows資格情報を追加するにはどうすればよいですか?

apacheのWinHttpClientsという簡単な例を作成し、そのサイトで使用されている自己署名crtも受け入れています。

これらは私の依存関係

dependencies { 
compile 'org.apache.httpcomponents:httpclient:4.5.+' 
compile 'org.apache.httpcomponents:httpclient-win:4.5.+' 

testCompile group: 'junit', name: 'junit', version: '4.11' 
} 

があり、私は私が手Apacheのラクダを通じて同じサイトにアクセスしようとすると、これは作業コード(認証が機能し、CRTの作品の受け入れ)

public class Application { 

    public static void main(String[] args) throws IOException { 

    if (WinHttpClients.isWinAuthAvailable()) { 
     PoolingHttpClientConnectionManager httpClientConnectionManager = new PoolingHttpClientConnectionManager(
     buildSSLSocketFactory()); 
     HttpClientBuilder clientBuilder = WinHttpClients.custom().useSystemProperties(); 
     clientBuilder.setConnectionManager(httpClientConnectionManager); 
     CloseableHttpClient httpClient = clientBuilder.build(); 

     HttpHost httpHost = new HttpHost("server.evilcorp.com", 443, "https"); 
     HttpGet httpGet = new HttpGet(
     "/evilwebapi/streams/endpointalpha/data"); 
     httpGet.setHeader("accept", "application/json"); 

     CloseableHttpResponse httpResponse = httpClient.execute(httpHost, httpGet); 

     String content = EntityUtils.toString(httpResponse.getEntity()); 
     System.out.println(content); // returns expected json result 
    } 
    } 

    private static Registry<ConnectionSocketFactory> buildSSLSocketFactory() { 
    SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(buildSSLContext(), NoopHostnameVerifier.INSTANCE); 
    return RegistryBuilder.<ConnectionSocketFactory>create() 
     .register("http", PlainConnectionSocketFactory.getSocketFactory()) 
     .register("https", sslSocketFactory) 
     .build(); 
    } 

    private static SSLContext buildSSLContext() { 
    SSLContext sslContext = null; 
    try { 
     sslContext = new SSLContextBuilder().loadTrustMaterial(null, (TrustStrategy) (arg0, arg1) -> true).build(); 
    } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) { 
     System.out.println("Failed to initialize SSL handling.\n" + e); 
    } 
    return sslContext; 
    } 
} 

です401ステータス。

私はラクダのhttpComponentをさまざまな方法で設定しようとしましたが、認証作業を行うことができません。これは現在のラクダの設定です。

これら

は私の依存関係です:

dependencies { 
    compile 'org.apache.camel:camel-core:2.18.+' 
    compile 'org.apache.camel:camel-sql:2.18.+' 
    compile 'org.apache.camel:camel-http4:2.18.+' 
    compile 'org.apache.camel:camel-jetty:2.18.+' 
    compile 'org.apache.camel:camel-jackson:2.18.+' 
    compile 'org.apache.camel:camel-guava-eventbus:2.18.+' 
    compile 'org.apache.camel:camel-quartz2:2.18.+' 
    compile 'com.fasterxml.jackson.core:jackson-core:2.7.+' 
    compile 'org.apache.httpcomponents:httpclient:4.5.+' 
    compile 'org.apache.httpcomponents:httpclient-win:4.5.+' 
    testRuntime files('src/test/resources') 
    runtime files('src/main/resources') 
} 

そして、これは動作しませんRouteBuilder(作品doesm't承認、からstatusCode:401)である

context = new DefaultCamelContext(registry); 
PropertiesComponent pc = new PropertiesComponent(); 
pc.setLocation("classpath:model.properties"); 
context.addComponent("properties", pc); 
try { 

    context.addRoutes(new RouteBuilder() { 
    public void configure() { 
     HttpComponent httpComponent = getContext().getComponent("https4", HttpComponent.class); 
     httpComponent.setHttpClientConfigurer(new WinHttpClientConfigurer()); 
     httpComponent.setClientConnectionManager(new PoolingHttpClientConnectionManager(WinHttpClientConfigurer.buildSSLSocketFactory())); 
     httpComponent.setHttpConfiguration(buildHttpConfiguration()); 
     getContext().getProperties().put("CamelJacksonEnableTypeConverter", "true"); 
     getContext().getProperties().put("CamelJacksonTypeConverterToPojo", "true"); 

     from("quartz2://pipull?cron=0+0/1+*+1/1+*+?+*") 
     .setHeader(Exchange.HTTP_QUERY, 
      simple("start='${header.start}'&end='${header.end}'")) 
     .multicast().parallelProcessing() 
     .to("direct:model"); 

     from("direct:model") 
     .setHeader("contractRef", simple("${properties:model.name}")) 
     .to("https4://server.evilcorp.com/evilwebapi/streams/endpointalpha/data") 
     .to("direct:transform"); 

     from("direct:transform").unmarshal() 
     .json(JsonLibrary.Jackson, Model.class) 
     .bean(ProcessorImpl.class) 
     .to("guava-eventbus:botBus"); 
     } 

     private HttpConfiguration buildHttpConfiguration() { 
     WindowsCredentialsProvider credentialsProvider = new WindowsCredentialsProvider(
      new SystemDefaultCredentialsProvider()); 
     Credentials credentials = credentialsProvider.getCredentials(new AuthScope(null, -1, null, AuthSchemes.NTLM)); 
     HttpConfiguration httpConfiguration = new HttpConfiguration(); 
     httpConfiguration.setAuthMethod(AuthSchemes.NTLM); 
     httpConfiguration.setAuthUsername(credentials.getUserPrincipal().getName()); 

     return httpConfiguration; 
     } 

    }); 
    context.start(); 
    } catch (Exception e) { 
    isRunning.set(false); 
    throw new RuntimeException(e); 
    } 
+0

NTLMを使用している場合、プリエンプティブ認証を行う必要がありますか? Camelエンドポイントが送信しているヘッダーを確認するために、テストサーバーに対してCamelルートを試しましたか? Camelが認証ヘッダーを送信していますか? –

答えて

0

私はサブタイプを通じて問題を解決してきましたHttpComponentとそれをラクダの文脈に追加する。

public class WinHttpComponent extends HttpComponent { 
    private static final Logger LOG = LoggerFactory.getLogger(WinHttpComponent.class); 

    public WinHttpComponent() { 
    this(HttpEndpoint.class); 
    } 

    public WinHttpComponent(Class<? extends HttpEndpoint> endpointClass) { 
    super(endpointClass); 
    } 

    @Override protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { 
     // copy-paste everything from super method 
     // replace this 
     // HttpClientBuilder clientBuilder = HttpClientBuilder.create(); 
     // with this 
     HttpClientBuilder clientBuilder = WinHttpClients.custom().useSystemProperties(); 
     // copy-paste everything from super method 
    } 
} 

context = new DefaultCamelContext(registry); 
context.addComponent("https4", new WinHttpComponent()); 
try { 
    context.addRoutes(new RouteBuilder() { 
    public void configure() { 
     HttpComponent httpComponent = getContext().getComponent("https4", HttpComponent.class); 
     // connection manager which accepts self-signed cert 
     httpComponent.setClientConnectionManager(new PoolingHttpClientConnectionManager(
     NoopSslVerifierHttpClientConfigurer.buildSSLSocketFactory())); 
     ... 
     ... 
     ... 
    } 
    } 
関連する問題