2013-07-11 56 views
2

我目前正在開發一個Java項目,但無法獲取http摘要身份驗證的工作。我嘗試使用Apache網站,但它沒有幫助。我有一個需要HTTP摘要認證的網站。使用Java的Apache Http摘要身份驗證

 DefaultHttpClient httpclient = new DefaultHttpClient(); 
     String hostUrl = "http://somewebsite.com"; 
     String postUrl = "http://somewebsite.com/request"; 
     HttpPost httpPost = new HttpPost(postUrl); 
     String username = "hello"; 
     String password = "world"; 
     HttpHost targetHost = new HttpHost(hostUrl); 

     httpclient.getCredentialsProvider().setCredentials(
       new AuthScope(hostUrl, AuthScope.ANY_PORT), 
       new UsernamePasswordCredentials(username, password)); 

     AuthCache authCache = new BasicAuthCache(); 

     DigestScheme digestAuth = new DigestScheme(); 

     digestAuth.overrideParamter("realm", "some realm"); 

     digestAuth.overrideParamter("nonce", "whatever"); 
     authCache.put(targetHost, digestAuth); 

     BasicHttpContext localcontext = new BasicHttpContext(); 
     localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache); 

     // List<NameValuePair> nvps = new ArrayList<NameValuePair>(); 
     // nvps.add(new BasicNameValuePair("username", "[email protected]")); 
     // nvps.add(new BasicNameValuePair("password", "example")); 
     // httpPost.setEntity(new UrlEncodedFormEntity(nvps)); 
     HttpResponse response2 = httpclient.execute(httpPost); 
+0

對於任何解決方案? –

回答

0

嘗試從Apache這個代碼:

public static void main(String[] args) throws Exception { 
      HttpClient client = new HttpClient(); 
      client.getState().setCredentials(
       new AuthScope("myhost", 80, "myrealm"), 
       new UsernamePasswordCredentials("username", "password")); 
      // Suppose the site supports several authetication schemes: NTLM and Basic 
      // Basic authetication is considered inherently insecure. Hence, NTLM authentication 
      // is used per default 

      // This is to make HttpClient pick the Basic authentication scheme over NTLM & Digest 
      List authPrefs = new ArrayList(3); 
      authPrefs.add(AuthPolicy.BASIC); 
      authPrefs.add(AuthPolicy.NTLM); 
      authPrefs.add(AuthPolicy.DIGEST); 
      client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authrefs); 

      GetMethod httpget = new GetMethod("http://myhost/protected/auth-required.html"); 

      try { 
       int status = client.executeMethod(httpget); 
       // print the status and response 
       System.out.println(httpget.getStatusLine()); 
       System.out.println(httpget.getResponseBodyAsString()); 
      } finally { 
       // release any connection resources used by the method 
       httpget.releaseConnection(); 
      }    
     } 
+0

這不起作用。 GetMethod不存在了。 – user2351234

1

嘗試從Apache的HttpClient的這段代碼4.3.3

final HttpHost targetHost = new HttpHost("localhost", 8080, "http"); 
    final CredentialsProvider credsProvider = new BasicCredentialsProvider(); 
    credsProvider.setCredentials(AuthScope.ANY, 
      new UsernamePasswordCredentials(user, password)); 

    final AuthCache authCache = new BasicAuthCache(); 
    DigestScheme digestAuth = new DigestScheme(); 
    digestAuth.overrideParamter("realm", "some-realm"); 
    digestAuth.overrideParamter("nonce", "whatever"); 
    authCache.put(targetHost, digestAuth); 

    // Add AuthCache to the execution context 
    HttpClientContext context = HttpClientContext.create(); 
    context.setAuthCache(authCache); 
HttpGet httpget = new HttpGet("/"); 
CloseableHttpResponse response = httpclient.execute(targetHost , httpget, context); 

請你能給我需要HTTP摘要式身份驗證的網站?

+0

你有沒有發現任何在線web服務來測試你的東西? –

+0

我使用http://httpbin.org。有一個摘要auth端點要測試。但是我仍然使用上面的代碼獲得401。這段代碼是否適用於使用Digest Auth的人? – jamiltz

2

此代碼的工作對我來說相當不錯:

protected static void downloadDigest(URL url, FileOutputStream fos) 
    throws IOException { 
    HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol()); 
    CloseableHttpClient httpClient = HttpClients.createDefault(); 
    HttpClientContext context = HttpClientContext.create(); 

    String credential = url.getUserInfo(); 
    if (credential != null) { 
    String user = credential.split(":")[0]; 
    String password = credential.split(":")[1]; 

    CredentialsProvider credsProvider = new BasicCredentialsProvider(); 
    credsProvider.setCredentials(AuthScope.ANY, 
     new UsernamePasswordCredentials(user, password)); 
    AuthCache authCache = new BasicAuthCache(); 
    DigestScheme digestScheme = new DigestScheme(); 
    authCache.put(targetHost, digestScheme); 

    context.setCredentialsProvider(credsProvider); 
    context.setAuthCache(authCache); 
    } 

    HttpGet httpget = new HttpGet(url.getPath()); 

    CloseableHttpResponse response = httpClient.execute(targetHost, httpget, context); 

    try { 
    ReadableByteChannel rbc = Channels.newChannel(response.getEntity().getContent()); 
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); 
    } finally { 
    response.close(); 
    } 
}