2011-07-01 20 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クライアントを使用してHTTPクライアントを設定しているようではありません。
あなたは(少なくとも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をフィルタリングしてトラフィックを確認します。しかし、問題はすでに間違っています。クライアントからの接続でSSLを使用していません。 – Cratylus

+0

またサイトにはsiteminder認証が必要です。 ? – ferhan

0

"realm"がそのAuthScopeコンストラクタの適切な値であることがわかっていない限り、それを削除するか、実際の値がどうあるべきかを判断することをおすすめします。

+0

私は1つのモード疑惑を持っています。私は私のユーザー名そのjspページのパスワードを入力します..次に、そのページにログインするための資格情報を渡す他の方法はありますか?そのJSPページのユーザー名とパスワードを渡すことが最良の方法ではありません...誰ですか?ファイルして参照してくださいユーザー名とパスワード??他の選択肢になることができることを教えてください... – ferhan

+0

あなたが達成しようとしていることを正確に把握していなければ、ユーザー名とパスワードを入力する方法はまだあります。また、かなりのJavaコードをJSPに入れておくと、すぐにメンテナンスが難しくなります。 – laz

+0

私がやろうとしているのは、上記のjspページを使って1ページの内容を取得しようとしていて、そのページに認証が必要だからです。そのJSPページのユーザー名とパスワードを渡すのは良い方法ではありません。だから他の方法..セッションのようないくつかの並べ替え??誰もがそのファイルを開くことでそのユーザー名とパスワードを見ることができるからです。 – ferhan

0

変更次の行を次の行にnew AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "realm")

new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM) 
関連する問題