2017-03-06 2 views
0

REST API에 액세스 할 때 자신을 인증해야합니다.아파치 낙타 루트에 windows 자격증 명을 추가하는 방법은 무엇입니까?

나는 아파치의 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' 
} 

은 그리고 내가 얻을 아파치 낙타를 통해 같은 사이트에 액세스하려고 할 때이 작업 코드 (인증 작업, 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())); 
     ... 
     ... 
     ... 
    } 
    }