Apache Axis2 1.5.1和NTLM身份验证

时间:2010-06-09 18:36:13

标签: java iis axis2 ntlm

我已经在StackOverflow上浏览了关于NTLM和Java的所有讨论,我似乎无法找到答案。我会尝试更加具体。

这里有一些代码返回一个客户端存根(我希望)配置为NTLM身份验证:

ServiceStub getService() {
  try {
    ServiceStub stub = new ServiceStub(
        "http://myserver/some/path/to/webservices.asmx"); // this service is hosted on IIS
    List<String> ntlmPreferences = new ArrayList<String>(1);
    ntlmPreferences.add(HttpTransportProperties.Authenticator.NTLM);
    HttpTransportProperties.Authenticator ntlmAuthenticator = new HttpTransportProperties.Authenticator();
    ntlmAuthenticator.setAuthSchemes(ntlmPreferences);
    ntlmAuthenticator.setUsername("me");
    ntlmAuthenticator.setHost("localhost");
    ntlmAuthenticator.setDomain("mydomain");
    Options options = stub._getServiceClient().getOptions();
    options.setProperty(HTTPConstants.AUTHENTICATE, ntlmAuthenticator);
    options.setProperty(HTTPConstants.CHUNKED, "false");
    return stub;
  } catch (AxisFault e) {
      e.printStackTrace();
  }
      return null;
}

返回有效的SerivceStub对象。当我尝试在存根上调用一个调用时,我在日志中看到以下内容:

Jun 9, 2010 12:12:22 PM org.apache.commons.httpclient.auth.AuthChallengeProcessor selectAuthScheme
INFO: NTLM authentication scheme selected
Jun 9, 2010 12:12:22 PM org.apache.commons.httpclient.HttpMethodDirector authenticate
SEVERE: Credentials cannot be used for NTLM authentication: org.apache.commons.httpclient.UsernamePasswordCredentials

有没有人能解决这个问题?

2 个答案:

答案 0 :(得分:3)

试试这个:http://robaustin.wikidot.com/axis 这个对我有用。您需要在getService()

之前调用setupCertsAndCredential()
private void setupCredential() {
  final NTCredentials nt = new NTCredentials("user", "pass", "", "domain");
  final CredentialsProvider myCredentialsProvider = new CredentialsProvider() {
   public Credentials getCredentials(AuthScheme scheme, String host, int port, boolean proxy) throws CredentialsNotAvailableException {
    return nt;
   }
  };
  DefaultHttpParams.getDefaultParams().setParameter("http.authentication.credential-provider", myCredentialsProvider);
 }

ServiceStub getService() {  

  try {   
    ServiceStub stub = new ServiceStub(   
        "http://myserver/some/path/to/webservices.asmx"); // this service is hosted on IIS   

    return stub;    
  } catch (AxisFault e) {    
      e.printStackTrace();    
  }    
      return null;    
}    

答案 1 :(得分:0)

HttpClient不支持NTLM v2因此我使用JCIFS库返回NTLM v1,2,3消息类型,如本网站所述

http://devsac.blogspot.com/2010/10/supoprt-for-ntlmv2-with-apache.html

我刚刚使用上述网站上的JCIFS_NTLMScheme.java文件注册了auth方案,它确实有效!!!!

示例客户端:

List authSchema = new ArrayList();
AuthPolicy.registerAuthScheme(AuthPolicy.NTLM, org.tempuri.JCIFS_NTLMScheme.class);
HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator();
auth.setUsername("");
auth.setPassword("");
auth.setDomain("");
auth.setHost("");
auth.setPort();
List authPrefs = new ArrayList(1);
authPrefs.add(AuthPolicy.NTLM);
auth.setAuthSchemes(authPrefs);
stub._getServiceClient().getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.AUTHENTICATE, auth); 
相关问题