2011-07-01 6 views
1
Updated Code:- Using SSL, still am getting the same error.. 
나는이 URI

HTTP/1.1 401 필요한 권한을 가진 HttpClient를 4.1.1

https://some-host/a/getmeta?id=10 (this url is passed to proxi.jsp page) 

을 열려고하고

그리고 이것은 내 proxi.jsp 페이지입니다, 그리고이 오류 HTTP/1.1 401 무엇입니까 인증 필요 및 자격 증명을 통과 할 때도 필요합니다. 왜 그런 일이 일어나고 .. 그 사이트는 siteminder를 사용합니다.

<%@ page language="java" import=" 
org.apache.http.HttpEntity, 
org.apache.http.HttpResponse, 
org.apache.http.auth.AuthScope, 
org.apache.http.auth.UsernamePasswordCredentials, 
org.apache.http.client.methods.HttpPost, 
org.apache.http.client.methods.HttpGet, 
org.apache.http.impl.client.DefaultHttpClient, 
org.apache.http.util.EntityUtils, 
java.io.InputStream, 
java.io.InputStreamReader, 
java.io.BufferedReader, 
java.security.KeyStore, 
java.io.FileInputStream, 
java.io.File, 
org.apache.http.conn.ssl.SSLSocketFactory, 
org.apache.http.conn.scheme.Scheme, 
javax.net.ssl.HostnameVerifier, 
org.apache.http.impl.conn.SingleClientConnManager, 
javax.net.ssl.HttpsURLConnection, 
org.apache.http.conn.scheme.SchemeRegistry, 
javax.net.ssl.SSLContext, 
java.security.cert.X509Certificate, 
javax.net.ssl.X509TrustManager, 
javax.net.ssl.TrustManager, 
org.apache.http.conn.ClientConnectionManager, 
java.security.cert.CertificateException, 
org.apache.http.conn.scheme.Scheme" 
contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 

    <% 
    String a_Url = request.getParameter("url") ; 

    DefaultHttpClient httpclient = new DefaultHttpClient(); 
    try { 
     httpclient.getCredentialsProvider().setCredentials(
       new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "realm"), 
       new UsernamePasswordCredentials("test", "pass")); 


     KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); 
     //FileInputStream instream = new FileInputStream(new File("my.keystore")); 
     InputStream instream = Thread.currentThread().getContextClassLoader().getResourceAsStream("my.keystore"); 
     try { 
      trustStore.load(instream, "nopassword".toCharArray()); 
     } finally { 
      try { instream.close(); } catch (Exception ignore) {} 
     } 
    /* 
     SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore); 
     Scheme sch = new Scheme("https", 443, socketFactory); 
     httpclient.getConnectionManager().getSchemeRegistry().register(sch); 
     */ 



     SSLContext ctx = SSLContext.getInstance("TLS"); 
     X509TrustManager tm = new X509TrustManager() { 

     public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException { 
     } 

     public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException { 
     } 

     public X509Certificate[] getAcceptedIssuers() { 
     return null; 
     } 
     }; 
     ctx.init(null, new TrustManager[]{tm}, null); 
     SSLSocketFactory ssf = new SSLSocketFactory(ctx); 
     ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 
     ClientConnectionManager ccm = httpclient.getConnectionManager(); 
     SchemeRegistry sr = ccm.getSchemeRegistry(); 
     sr.register(new Scheme("https", ssf, 443)); 




     HttpGet httpget = new HttpGet(a_Url); 



     System.out.println("executing request" + httpget.getRequestLine()); 
     HttpResponse res = httpclient.execute(httpget); 

     HttpEntity entity = res.getEntity(); 

     System.out.println("----------------------------------------"); 
     System.out.println(res.getStatusLine()); 
     if (entity != null) { 

      System.out.println("Response content length: " + entity.getContentLength()); 
      InputStream input = entity.getContent(); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(input)); 
      String ln = ""; 
      while((ln = reader.readLine()) != null) { 
       out.println("During Get - " + ln); 
      } 
      entity.consumeContent(); 
     } 
     EntityUtils.consume(entity); 
    } 

    catch (Throwable t) { 
     StackTraceElement[] x = t.getStackTrace(); 
     for(int k=0;k<x.length;k++) { 
      out.println(x[k].toString()); 
     } 
     //out.println(); 
     t.printStackTrace(); 
    } 


    finally { 
     // When HttpClient instance is no longer needed, 
     // shut down the connection manager to ensure 
     // immediate deallocation of all system resources 
     httpclient.getConnectionManager().shutdown(); 
    } 


    %> 
+0

당신이 자격 증명이 올바른지 특정 있습니까? – laz

+0

@ laz, 맞습니다. 코드에 문제가 있습니까? ?? – ferhan

답변

0

보안 사이트에 액세스하고 있지만 HttpClient 코드에서 SSL 처리가 표시되지 않습니다. this page을보고 적절한 간격을 채운 후 독립 실행 형 클라이언트에서 사용해 볼 수 있습니까?

+0

나는 4.1.1을하지 않고 3.1을 사용하고있다. 다른 제안? – ferhan

+0

이 링크의 SSL/TLS 사용자 정의 섹션 : http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e523이 도움이 될 수 있습니다. –

+0

별로 도움이되지 않습니다. ( – ferhan

0

먼저 게시 한 코드에서 HTTP 클라이언트가 HTTPS를 사용하도록 구성하지 않은 것으로 보입니다.
당신은 (적어도 org.apache.http.client.HttpClient에 대한) 다음과 같은 코드가 누락되었습니다 :

SSLSocketFactory sf = new SSLSocketFactory(sslcontext); 
Scheme https = new Scheme("https", sf, 443); 
httpclient.getConnectionManager().getSchemeRegistry().register(https); 

당신은 무슨 일이 일어나고 있는지보고 어떤 경우 DefaultHttpClient
에 대한 자습서를 확인해야합니다, 당신은 같은 스니핑 도구를 사용할 수 있습니다 wireshark.
SSL 핸드 셰이크를 볼 수 있으므로 연결 실패를보고 왜 그 이유를 알 수 있습니다.

+0

어떻게 wireshark를 사용할 수 있습니까 .. 어떤 제안을 주시면 감사하겠습니다 .. – ferhan

+0

무슨 뜻입니까? wireshark (http://www.wireshark.org/download.html)를 시작하십시오. 캡처를 시작하여 클라이언트 및 서버의 IP를 필터링하고 트래픽을 확인하십시오. 그러나 문제는 이미 잘못된 것입니다. 클라이언트 – Cratylus

+0

과의 연결에서 SSL을 사용하지 않고 해당 사이트에 siteminder 인증이 필요합니다. ?? – ferhan

0

해당 이 해당 AuthScope 생성자에서 적절한 값인 지 확실하지 않으면 제거하거나 실제 값을 결정할 것을 권합니다.

+0

하나의 모드에 대한 의문이 있습니다. 사용자 이름을 사용하고 싶지 않다면 그 jsp 페이지에서 암호를 .. 그런 다음 그 페이지에 로그인하기 위해 자격 증명을 전달하는 다른 방법이 있습니까? JSP 페이지에서 사용자 이름과 passowrd를 전달하는 것이 최선의 방법은 아닌 것처럼 ... 맞습니까? 누구든지 열 수 있습니다. 파일을보고 사용자 이름과 암호 ??. 다른 옵션이 무엇인지 알리십시오 ... – ferhan

+0

정확히 무엇을 성취하려고하는지 알지 못해도 사용자 이름과 암호를 제공하는 다른 방법이 있습니다. 또한 JSP 코드에 꽤 많은 Java 코드를 넣으므로 유지 관리가 어려워 질 수 있습니다. – laz

+0

내가 뭘 하려는지 단순히 위의 JSP 페이지를 사용하여 한 페이지의 내용을 얻으려고하고 해당 페이지는 인증이 필요합니다 .. 그래서 JSP 페이지에서 사용자 이름과 암호를 전달하는 것이 좋은 방법이 아닙니다. " 그래서 다른 방법으로 .. 세션 같은 일종의 ?? 누구든지 해당 파일을 열어서 사용자 이름과 암호를 볼 수 있기 때문입니다. – ferhan

0

변경 다음 줄 : 다음 줄에 new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "realm")

:

new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM) 
관련 문제