axis2 client NTLM authentication

前端 未结 4 651
闹比i
闹比i 2020-12-20 02:29

I have an axis2 (v1.5.3) client that needs to do Kerberos/NTLM authentication with IIS. How can I do this? This is the code I have right now and it fails with 401 - un

4条回答
  •  Happy的楠姐
    2020-12-20 02:46

    An alternative to JCIFS is to use the Apache HTTPComponents 4 NTLMScheme (which works with new NTLM) inside a custom Apache Commons HTTP AuthScheme:

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

    Usage

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

    I tested this on IIS 7.5 on Windows Server 2008 R2.

提交回复
热议问题