2011-04-21 93 views
10

这里的问题是在客户端使用Apache HttpClient时使用具有NTLM身份验证的Web资源。我遇到的问题是强制客户端使用NTLM身份验证。这里是一个代码sapmle。Apache HttpClient 4.1.1 NTLM身份验证不是SPNEGO

DefaultHttpClient httpclient = new DefaultHttpClient(); 
httpclient.getAuthSchemes().register("ntlm",new NTLMSchemeFactory()); 
NTCredentials creds = new NTCredentials("_myUSer_","_myPass_","_myWorkstation_","_myDomain_"); 
httpclient.getCredentialsProvider().setCredentials(new AuthScope("serverName",80), creds); 
List<String> authpref = new ArrayList<String>(); 
authpref.add(AuthPolicy.NTLM); 
httpclient.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, authpref); 
HttpHost target = new HttpHost("serverName", 80, "http"); 
HttpGet httpget = new HttpGet("webResource"); 
HttpContext localContext = new BasicHttpContext(); 
HttpResponse response = httpclient.execute(target, httpget, localContext); 

以下是错误从Java:

org.apache.http.client.protocol.RequestTargetAuthentication process 
SEVERE: Authentication error: Invalid name provided (Mechanism level: Could not load configuration file C:\WINDOWS\krb5.ini (The system cannot find the file specified)) 

Web服务器响应是401

有关为什么auth策略未正确设置的任何想法? 我在代码中丢失了什么吗?

+1

我发现一个问题,我的代码,这是该AuthScope应指向您的代理,而不是你的目标,这并摆脱它试图使用Kerberos而不是NTLM的错误,但我仍然从服务器获得401,正确的用户名/密码/域组合的任何想法? – Kelly 2011-04-21 16:35:50

+1

HttpClient需要更新检查我的帖子[http://stackoverflow.com/questions/5917356/httpclient-4-1-1-returns-401-when-authenticating-with-ntlm-browsers-work-fine/20047880# 20047880] [1] [1]:http://stackoverflow.com/questions/5917356/httpclient-4-1-1-returns-401-when-authenticating-with-ntlm-browsers-work - 精细/ 20047880#20047880 – 2013-11-19 11:43:09

回答

0

我认为这是因为缺陷,请参见here

5

我有类似的情况,我怀疑你设置了错误的参数:AuthPNames.PROXY_AUTH_PREF。我使用AuthPNames.TARGET_AUTH_PREF和所有似乎工作正常。

2

这是我对这个问题的解决方案:“evandongen”是正确的。

请注意使用URIBuilder。

String username = "uid"; 
String pwd = "pwd"; 
String servername = "www.someserver.com"; 
String workstation = "myworkstation"; 
String domain = "somedomain"; 
String relativeurl = "/util/myservice.asmx"; 

String oldimagePath = "\\mypath\\image.jpg"; 

DefaultHttpClient httpclient = new DefaultHttpClient(); 

try { 
    httpclient.getAuthSchemes().register("ntlm",new NTLMSchemeFactory()); 
    NTCredentials creds = new NTCredentials(username,pwd,workstation,domain); 

     httpclient.getCredentialsProvider().setCredentials(new AuthScope(servername,80), creds); 

     List authpref = new ArrayList(); 

     authpref.add(AuthPolicy.NTLM); 

     URIBuilder builder = new URIBuilder(); 
     builder.setScheme("http") 
      .setHost(servername) 
      .setPath(relativeurl + "/DeleteImage") 
      .setParameter("imagePath", oldimagePath); 
     URI uri = builder.build(); 

     httpclient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, authpref); 
     HttpHost target = new HttpHost(servicename, 80, "http"); 
     HttpGet httpget = new HttpGet(uri); 

     HttpContext localContext = new BasicHttpContext(); 

     HttpResponse response1 = httpclient.execute(target, httpget, localContext); 

     BufferedReader reader = new BufferedReader(new InputStreamReader(response1.getEntity().getContent())); 

     String line = reader.readLine(); 
     while (line != null) 
     { 
      System.out.println(line); 
      line = reader.readLine(); 
     } 

} catch (Exception e) { 
    System.out.println("Exception:"+e.toString()); 
} finally { 
    // End 
} 
0

HttpClient没有为我工作,但下面的代码段。 参考 - http://docs.oracle.com/javase/7/docs/technotes/guides/net/http-auth.html

public static String getResponse(String url, String userName, String password) throws IOException { 
    Authenticator.setDefault(new Authenticator() { 
     @Override 
     public PasswordAuthentication getPasswordAuthentication() { 
     System.out.println(getRequestingScheme() + " authentication"); 
     return new PasswordAuthentication(userName, password.toCharArray()); 
     } 
    }); 

    URL urlRequest = new URL(url); 
    HttpURLConnection conn = (HttpURLConnection) urlRequest.openConnection(); 
    conn.setDoOutput(true); 
    conn.setDoInput(true); 
    conn.setRequestMethod("GET"); 

    StringBuilder response = new StringBuilder(); 
    InputStream stream = conn.getInputStream(); 
    BufferedReader in = new BufferedReader(new InputStreamReader(stream)); 
    String str = ""; 
    while ((str = in.readLine()) != null) { 
     response.append(str); 
    } 
    in.close(); 

    return response.toString(); 
    }