【问题标题】:NTLM solution for RESTful web services in JAVAJAVA 中 RESTful Web 服务的 NTLM 解决方案
【发布时间】:2013-01-15 11:56:07
【问题描述】:

我正在为 Restful Web 服务编写客户端。

public static void main(String[] args){
        // Use apache commons-httpclient to create the request/response
        HttpClient client = new HttpClient();
        Credentials defaultcreds = new UsernamePasswordCredentials("aaa", "cdefg");
        client.getState().setCredentials(AuthScope.ANY, defaultcreds);


        GetMethod method = new GetMethod(
                "http://localhost:8080/userService/usersByID/1234");
        try {
            client.executeMethod(method);
            InputStream in = method.getResponseBodyAsStream();
            // Use dom4j to parse the response and print nicely to the output stream
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder out = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                out.append(line);
            }
            System.out.println(out.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } 
    }

我收到未经授权的错误 401。当我检查服务器日志时。我找到了以下内容。

ERROR httpclient.HttpMethodDirector - Credentials cannot be used for NTLM authentication: org.apache.commons.httpclient.UsernamePasswordCredentials

所以,我知道我需要为此使用 NTLM 身份验证。

谁能告诉我如何修改它以进行 NTLM 身份验证。

提前致谢。

【问题讨论】:

标签: java web-services rest cxf ntlm


【解决方案1】:

请查看以下链接

http://hc.apache.org/httpclient-3.x/authentication.html#NTLM http://davenport.sourceforge.net/ntlm.html

【讨论】:

    【解决方案2】:

    尝试使用 setProxyCredentials 代替 setCredentials。

    client.getState().setProxyCredentials(AuthScope.ANY, defaultcreds);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多