本文介绍了使用Java的URLConnection NTLM(或Kerberos)认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要消耗用java休息web服务,通​​过一个域用户帐户的凭据。

I need to consume a rest web service with java, passing the credentials of a domain user account.

现在我用传统的ASP做

right now I'm doing it with classic asp


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

和asp.net

and with 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();

我怎么用java实现这一目标?考虑到,我不使用当前登录用户的凭据,我specifing域帐户(我有密码)

how can I achieve this with 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)

请告诉我这是与传统的ASP和asp.net容易....

please tell me it's as easy as with classic asp and asp.net....

推荐答案

看一看在SPNEGO HTTP Servlet过滤程序,项目SpnegoHttpURLConnection类。这个项目有一些例子也是如此。

Take a look at the SpnegoHttpURLConnection class in the SPNEGO HTTP Servlet Filter project. This project has some examples as well.

该项目具有是pretty钱?你在你的例子在做什么。

This project has a client library that pretty much does what you are doing in your example.

从的Javadoc看看这个例子...

Take a look this example from the 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());
 }

这篇关于使用Java的URLConnection NTLM(或Kerberos)认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 06:40