【问题标题】:axis2 client NTLM authenticationaxis2 客户端 NTLM 身份验证
【发布时间】:2011-08-17 10:57:37
【问题描述】:

我有一个axis2 (v1.5.3) 客户端,需要使用IIS 进行Kerberos/NTLM 身份验证。我怎样才能做到这一点?这是我现在拥有的代码,它失败并出现401 - unauthorized 错误:

List<String> authScheme = new ArrayList<String>();
authScheme.add(HttpTransportProperties.Authenticator.NTLM);
HttpTransportProperties.Authenticator ntlm =
                 new HttpTransportProperties.Authenticator();
ntlm.setAuthSchemes(authScheme);
ntlm.setUsername("Administrator");
ntlm.setPassword("password");
ntlm.setHost("http://server/_vti_bin/someservice.asmx");
ntlm.setPort(80);
ntlm.setDomain("server_domain");
Options options = webs._getServiceClient().getOptions();
options.setProperty(HTTPConstants.AUTHENTICATE, ntlm);
stub._getServiceClient().setOptions(options);  

用 C# 编写的客户端可以在相同的身份验证设置下正常工作:

CredentialCache myCache = new CredentialCache();            
myCache.Add(new Uri(webs.Url), "NTLM", 
            new NetworkCredential("Administrator", "password", "server_domain"));
stub.Credentials = myCache;

【问题讨论】:

  • 嗨@Vijay Mathew,你解决了吗?我正在开发一项新服务来进行 NTLM 身份验证nafiux.com/wasp 如果我可以帮助您,请告诉我

标签: java web-services iis axis2 axis


【解决方案1】:

AXIS2 中的 NTLM 存在问题。它以 ntlm.setHost() 方法为中心。此处的条目既用作 NTLM 交换中的 WORKSTATION,又用作创建 AuthScope 时的远程主机。这会导致 NTLM 无法使用 HttpTransportProperties.Authenticator 技术工作的 Catch-22 情况。您要么收到“401 未授权”,要么收到“未找到 @HOST 的凭据”。

https://issues.apache.org/jira/browse/AXIS2-4595

彼得

【讨论】:

    【解决方案2】:

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

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

    我刚刚使用上述网站的 JCIFS_NTLMScheme.java 文件注册了身份验证方案,它成功了!!!!

    示例客户端:

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

    【讨论】:

      【解决方案3】:

      基于此链接NTLM issues with Axis2的注释

      Axis2 仍然使用旧的 HTTPClient 库,并且似乎该版本不支持 NTLM 的所有版本(v1、v2)。而且将传输切换到 HTTPClient v4.1 也并非易事

      我放弃了 Axis2 并改用了CXF

      以下链接确实让我们克服了 Kerboros/NTLM 问题

      http://download.oracle.com/javase/6/docs/technotes/guides/net/http-auth.html
      

      【讨论】:

        【解决方案4】:

        JCIFS 的替代方法是在自定义 Apache Commons HTTP AuthScheme 中使用 Apache HTTPComponents 4 NTLMScheme(works with new NTLM):

        public class BackportedNTLMScheme extends org.apache.http.impl.auth.NTLMScheme implements org.apache.commons.httpclient.auth.AuthScheme {
        
            @Override
            public String authenticate(final Credentials credentials, final HttpMethod method) throws AuthenticationException {
                org.apache.commons.httpclient.NTCredentials oldCredentials;
                try {
                    oldCredentials = (org.apache.commons.httpclient.NTCredentials) credentials;
                } catch (final ClassCastException e) {
                    throw new InvalidCredentialsException(
                            "Credentials cannot be used for NTLM authentication: " 
                            + credentials.getClass().getName());
                }
                final org.apache.http.auth.Credentials adaptedCredentials = new NTCredentials(oldCredentials.getUserName(), oldCredentials.getPassword(), oldCredentials.getHost(), oldCredentials.getDomain());
        
                try {
                    final Header header = super.authenticate(adaptedCredentials, null);
                    return header.getValue();
                } catch (final org.apache.http.auth.AuthenticationException e) {
                    throw new AuthenticationException("AuthenticationException", e);
                }
            }
        
            @Override
            public void processChallenge(final String challenge) throws MalformedChallengeException {
                final String s = AuthChallengeParser.extractScheme(challenge);
                if (!s.equalsIgnoreCase(getSchemeName())) {
                    throw new MalformedChallengeException("Invalid NTLM challenge: " + challenge);
                }
                int challengeIdx = challenge.indexOf(' ');
                final CharArrayBuffer challengeBuffer;
                if(challengeIdx != -1){
                    challengeBuffer = new CharArrayBuffer(challenge.length());
                    challengeBuffer.append(challenge);
                } else {
                    challengeBuffer = new CharArrayBuffer(0);
                    challengeIdx = 0;
                }
                try {
                    parseChallenge(challengeBuffer, challengeIdx, challengeBuffer.length());
                } catch (final org.apache.http.auth.MalformedChallengeException e) {
                    throw new MalformedChallengeException("MalformedChallengeException", e);
                }
            }
        
            @Override
            @Deprecated
            public String getID() {
                throw new RuntimeException("deprecated vc.bjn.catalyst.forecast.BackportedNTLMScheme.getID()");
            }
        
        
            @Override
            @Deprecated
            public String authenticate(final Credentials credentials, final String method, final String uri) throws AuthenticationException {
                throw new RuntimeException("deprecated vc.bjn.catalyst.forecast.BackportedNTLMScheme.authenticate(Credentials, String, String)");
            }
        }
        

        用法

        AuthPolicy.registerAuthScheme(AuthPolicy.NTLM, BackportedNTLMScheme.class);
        

        我在 Windows Server 2008 R2 上的 IIS 7.5 上对此进行了测试。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-10-13
          • 1970-01-01
          • 2020-03-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多