2011-06-30 39 views
2

由於我的工作是在打電話到URL的項目,該網址使用的SiteMinder所以每次我做出這個URL登錄的SiteMinder使用Java

https://some-host/a/getmeta?id=10 

請求重定向我包含SiteMinder的事情

location: https://login.somehost.com/siteminderagent/nocert/1309460767/smgetcre 
d.scc?TYPE=16777217&REALM=-SM-Documentum%20[12%3a06%3a07%3a4932]&SMAUTHREASON=0& 
METHOD=GET&SMAGENTNAME=-SM-6D9yKpar83ASDc5Sb4KDjZtHgfZId%2fYHFKbzwYvx5EUeGMi0dOa 
uGVx6wOk1daI3&TARGET=-SM-http%3A%2F%2Fsome-host%2Fa%2Fgetmeta%3Fid%3D10 

,如果我打開此鏈接到瀏覽器,然後它要求用戶名和密碼,如果我提供的用戶名和密碼,然後我得到我的實際內容回..

一些其他網址

因此,我如何每次驗證身份並使用HTTPCLIENT 4.1.1。

回答

2

那麼,你可以按照重定向,填寫字段併發布表單。它會帶你到你想去的地方。

這些字段可能是siteminder的標準USER和PASSWORD字段。你只需要確保你的java代碼正確處理重定向並維護cookie。 commons-httpclient很容易爲你做到這一點。

+0

我怎麼能抓住這個用戶名和密碼值,一個人試圖在彈出的輸入.. – ferhan

+0

是您的HttpClient的代碼的Web服務器中運行?你不能簡單地向他們提供憑證的表格嗎?一旦他們提交表單,您將在與後端服務器通信時使用用戶名和密碼。 –

+0

我無法控制該窗體..由於該彈出窗口是由SiteMinder控制的..所以我不能做任何事情..我認爲有一些方法在SM_SESSION通過我可以登錄每個用戶..任何想法.. – ferhan

0
<h1>I used below code to connect site minder authentication. Its working fine.</h1> 

<code> 
@SuppressWarnings("restriction") 
    public static void main(String[] args) throws IOException 
    { 
     disableSslVerification(); 

     CookieManager cookimngr = new CookieManager(null, CookiePolicy.ACCEPT_ALL); 
     CookieHandler.setDefault(cookimngr); 
     URL svrUrl = new URL("https://ABC_HOST:ABC_PORT/XYZ/"); 
     HttpURLConnection conn = (HttpURLConnection)svrUrl.openConnection(); 
     conn.setRequestProperty("Authorization", "Basic " + new sun.misc.BASE64Encoder().encode((username+":"+password).getBytes())); 

     InputStream in = conn.getInputStream(); 

     for (HttpCookie h : cookimngr.getCookieStore().getCookies()){ 
      System.out.println(h.getName()); 
      System.out.println(h.getValue()); 
     } 
     in.close(); 

    } 
/** disable the SSL Certificate Verification **/ 
    public static void disableSslVerification() { 
     try 
     { 
      // Create a trust manager that does not validate certificate chains 
      TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() { 
       public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
        return null; 
       } 
       public void checkClientTrusted(X509Certificate[] certs, String authType) { 
       } 
       public void checkServerTrusted(X509Certificate[] certs, String authType) { 
       } 
      } 
      }; 
      // Install the all-trusting trust manager 
      SSLContext sc = SSLContext.getInstance("SSL"); 
      sc.init(null, trustAllCerts, new java.security.SecureRandom()); 
      HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); 
      // Create all-trusting host name verifier 
      HostnameVerifier allHostsValid = new HostnameVerifier() { 
       public boolean verify(String hostname, SSLSession session) { 
        return true; 
       } 
      }; 
      // Install the all-trusting host verifier 
      HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); 
     } catch (NoSuchAlgorithmException e) { 
      e.printStackTrace(); 
     } catch (KeyManagementException e) { 
      e.printStackTrace(); 
     } 

    } 
</code>