简体   繁体   English

使用 java UrlConnection 对 ntlm(或 kerberos)进行身份验证

[英]authenticate with ntlm (or kerberos) using java UrlConnection

I need to consume a rest web service with java, passing the credentials of a domain user account.我需要使用 java 使用 rest web 服务,传递域用户帐户的凭据。

right now I'm doing it with classic asp现在我正在使用经典的 asp


set xmlHttp = server.createObject( "msxml2.serverxmlhttp" )
xmlHttp.open method, url, false, domain & "\" & user, password
xmlHttp.send body
out = xmlHttp.responseText
set xmlHttp = nothing

and with asp.net和 asp.net



HttpWebRequest request = (HttpWebRequest) WebRequest.Create( url );

request.Credentials = new NetworkCredential(user, password, domain);

request.Method = WebRequestMethods.Http.Get

HttpWebResponse response = (HttpWebResponse) request.GetResponse();

StreamReader outStream = new StreamReader( response.GetResponseStream(), Encoding.UTF8) ;

output = outStream.ReadToEnd();

how can I achieve this with java?我怎样才能用java实现这个? Take into account that I'm not using the credentials of the currently logged user, I'm specifing the domain account (I have the password)考虑到我没有使用当前登录用户的凭据,我正在指定域帐户(我有密码)

please tell me it's as easy as with classic asp and asp.net....请告诉我它和经典的 asp 和 asp.net 一样简单....

According to this page , you can use the built-in JRE classes, with the caveat that earlier versions of Java can only do this on a Windows machine.根据此页面,您可以使用内置的 JRE 类,但需要注意的是,早期版本的 Java 只能在 Windows 机器上执行此操作。

However, if you are willing to live with a 3rd-party dependency, IMO Apache Commons HttpClient 3.x is the way to go.但是,如果您愿意忍受第 3 方依赖项,IMO Apache Commons HttpClient 3.x是您的不二之选。 Here is the documentation for using authentication, including NTLM.是使用身份验证的文档,包括 NTLM。 In general, HttpClient is a much more functional library.一般来说,HttpClient 是一个功能更强大的库。

The latest version of HttpClient is 4.0, but HttpClient 的最新版本是 4.0,但是apparently this version does not support NTLM显然这个版本不支持 NTLM this version requires a tiny bit of extra work .这个版本需要一些额外的工作

Here is what I think the code would look like, although I haven't tried it:这是我认为代码的样子,尽管我还没有尝试过:

HttpClient httpClient = new HttpClient();
httpClient.getState().setCredentials(AuthScope.ANY, new NTCredentials(user, password, hostPortionOfURL, domain));
GetMethod request = new GetMethod(url);
BufferedReader reader = new InputStreamReader(request.getResponseBodyAsStream());

Good luck.祝你好运。

A compatible solution for java.net.URLStreamHandler and java.net.URL is com.intersult.net.http.NtlmHandler: java.net.URLStreamHandler 和 java.net.URL 的兼容解决方案是 com.intersult.net.http.NtlmHandler:

NtlmHandler handler = new NtlmHandler();
handler.setUsername("domain\\username");
handler.setPassword("password");
URL url = new URL(null, urlString, handler);
URLConnection connection = url.openConnection();

You also can use java.net.Proxy within url.openConnection(proxy).您还可以在 url.openConnection(proxy) 中使用 java.net.Proxy。

Use Maven-Dependency:使用 Maven 依赖:

    <dependency>
        <groupId>com.intersult</groupId>
        <artifactId>http</artifactId>
        <version>1.1</version>
    </dependency>

Take a look at the SpnegoHttpURLConnection class in the SPNEGO HTTP Servlet Filter project.查看 SPNEGO HTTP Servlet Filter 项目中的 SpnegoHttpURLConnection 类。 This project has some examples as well.这个项目也有一些例子。

This project has a client library that pretty much does what you are doing in your example.该项目有一个客户端库,它几乎可以完成您在示例中所做的工作。

Take a look this example from the javadoc...看看这个来自javadoc的例子......

 public static void main(final String[] args) throws Exception {
     final String creds = "dfelix:myp@s5";

     final String token = Base64.encode(creds.getBytes());

     URL url = new URL("http://medusa:8080/index.jsp");

     HttpURLConnection conn = (HttpURLConnection) url.openConnection();

     conn.setRequestProperty(Constants.AUTHZ_HEADER
             , Constants.BASIC_HEADER + " " + token);

     conn.connect();

     System.out.println("Response Code:" + conn.getResponseCode());
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM