【问题标题】:Apache Http Digest Authentication using Java使用 Java 的 Apache Http Digest 身份验证
【发布时间】:2013-07-11 17:02:51
【问题描述】:

我目前正在处理一个 Java 项目,但无法使 http 摘要身份验证正常工作。我尝试使用 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", "shirwa99@gmail.com"));
        // nvps.add(new BasicNameValuePair("password", "example"));
        // httpPost.setEntity(new UrlEncodedFormEntity(nvps));
        HttpResponse response2 = httpclient.execute(httpPost);

【问题讨论】:

  • 有什么解决办法吗?

标签: java apache md5 http-authentication digest


【解决方案1】:

这段代码很适合我:

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();
  }
}

【讨论】:

    【解决方案2】:

    从 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摘要认证的网站吗?

    【讨论】:

    • 你有没有找到任何在线网络服务来测试你的东西?
    • 我使用httpbin.org。有一个摘要身份验证端点要测试。但我仍然使用上面的代码返回 401。这个 sn-p 是否适用于使用 Digest Auth 的任何人?
    【解决方案3】:

    提示:不要使用 HTTP Digest :) 它根本不安全。通过 HTTPS 没有意义。

    如果必须,下面是用于解析 WWW-Authenticate 标头的代码。

    这是使用以下依赖项进行测试的(我使用gradle):

    compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.6'
    

    代码:

    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import org.apache.http.Header;
    import org.apache.http.HttpHost;
    import org.apache.http.auth.AuthScope;
    import org.apache.http.auth.MalformedChallengeException;
    import org.apache.http.auth.UsernamePasswordCredentials;
    import org.apache.http.client.AuthCache;
    import org.apache.http.client.CredentialsProvider;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.protocol.HttpClientContext;
    import org.apache.http.impl.auth.DigestScheme;
    import org.apache.http.impl.client.BasicAuthCache;
    import org.apache.http.impl.client.BasicCredentialsProvider;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    
    public class DigestExample {
    
      private final static String uri = "http://my.digest.based.auth.url.com";
      private static HttpHost target;
    
      public static void main(String[] args) throws IOException {    
    
        setup();
        if (target == null) {
          System.out.println("Setup was unsuccesfull");
          return;
        }
        Header challengeHeader = getAuthChallengeHeader();
        if (challengeHeader == null) {
          System.out.println("Setup was unsuccesfull");
          return;
        }
    
        // NOTE: challenge is reused for subsequent HTTP GET calls (typo corrected)
        getWithDigestAuth(challengeHeader, "/", "/schema");
    
      }
    
      private static void setup() throws MalformedURLException {
        URL url = new URL(uri);
        target = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
      }
    
      private static Header getAuthChallengeHeader() {
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
          CloseableHttpResponse response = httpClient.execute(new HttpGet(uri));
          return response.getFirstHeader("WWW-Authenticate");
        } catch (IOException e) {
          e.printStackTrace();
          return null;
        }
      }
    
      private static void getWithDigestAuth(Header challengeHeader, String... requests)
          throws IOException {
    
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
            new AuthScope(target.getHostName(), target.getPort()),
            new UsernamePasswordCredentials("user", "pass"));
    
        try (CloseableHttpClient httpclient = HttpClients.custom()
            .setDefaultCredentialsProvider(credsProvider)
            .build()) {
    
          // Create AuthCache instance
          AuthCache authCache = new BasicAuthCache();
          // Generate DIGEST scheme object, initialize it and add it to the local
          // auth cache
          DigestScheme digestAuth = new DigestScheme();
          digestAuth.processChallenge(challengeHeader);
          authCache.put(target, digestAuth);
    
          // Add AuthCache to the execution context
          HttpClientContext localContext = HttpClientContext.create();
          localContext.setAuthCache(authCache);
    
          for (String request : requests) {
            System.out.println("Executing request to target " + target + request);
            try (CloseableHttpResponse response = httpclient
                .execute(target, new HttpGet(request), localContext)) {
              System.out.println("----------------------------------------");
              System.out.println(response.getStatusLine());
              System.out.println(EntityUtils.toString(response.getEntity()));
            } catch (Exception e) {
              System.out.println("Error while executing HTTP GET request");
              e.printStackTrace();
            }
          }
        } catch (MalformedChallengeException e) {
          e.printStackTrace();
        }
      }
    }
    

    【讨论】:

      【解决方案4】:

      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();
                  }            
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-11
        • 2012-06-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-05
        相关资源
        最近更新 更多