【问题标题】:Apache HttpClient 4.1.1 NTLM authentication not SPNEGOApache HttpClient 4.1.1 NTLM 身份验证不是 SPNEGO
【发布时间】:2011-08-10 10:23:07
【问题描述】:

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

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))

网络服务器响应是401

关于为什么没有正确设置身份验证策略的任何想法? 我在代码中遗漏了什么吗?

【问题讨论】:

  • 我发现我的代码有一个问题,那就是 AuthScope 应该指向您的代理而不是您的目标,这确实消除了尝试使用 Kerberos 而不是 NTLM 的错误,但是我仍然从服务器收到 401,关于正确的用户名/密码/域组合的任何想法?

标签: java ntlm apache-httpclient-4.x


【解决方案1】:

我有类似的情况,我怀疑您设置了错误的参数: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
    }
    

    【讨论】:

      【解决方案3】:

      我认为这是由于缺陷造成的,请参阅here

      【讨论】:

        【解决方案4】:

        HttpClient 对我不起作用,但下面的 sn-p 起作用了。 参考——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();
          }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-01
          • 1970-01-01
          • 2020-07-17
          • 2018-05-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多