org.apache.axis2.AxisFault:传输错误:401错误:从WSO2 ESB调用NTLM服务时未经授权

时间:2017-05-15 23:34:54

标签: java wso2 wso2esb ntlm

我正在尝试从WSO2 ESB调用NTLM服务。我已经为NTLM访问创建了一个自定义中介,但每次都获得“org.apache.axis2.AxisFault:传输错误:401错误:未经授权”,我们将非常感谢任何帮助。

代码:

package mbie.poc;

import java.util.ArrayList;

import javax.xml.soap.SOAPEnvelope;

import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.OperationClient;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.transport.http.HttpTransportProperties;
import org.apache.axis2.wsdl.WSDLConstants;
import org.apache.commons.httpclient.auth.AuthPolicy;
import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;

public class NTLMAuthorisation extends AbstractMediator { 

    /* private String soapAction;
        private String soapEndpoint;
        private String domain;
        private String host;
        private int port;
        private String username;
        private String password;
*/
        public boolean mediate(MessageContext context) { 

            //Build NTLM Authentication Scheme

            System.out.println("Class code executing...");
            //AuthPolicy.registerAuthScheme(AuthPolicy.NTLM, JCIFS_NTLMScheme.class);
            HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator();
            auth.setUsername("XXXX");
            auth.setPassword("P@XX");
            auth.setDomain("XX");
            auth.setHost("XXXX");
            auth.setPort(80);
            auth.setPreemptiveAuthentication(true);
            ArrayList<String> authPrefs = new ArrayList<String>();
            authPrefs.add(AuthPolicy.NTLM);
            auth.setAuthSchemes(authPrefs);
            System.out.println("auth.NTLM   : "+authPrefs);



            //Force Authentication - failures will get caught in the catch block
            try {

                //Build ServiceClient and set Authorization Options
                ServiceClient serviceClient = new ServiceClient();
                Options options = new Options();
                options.setProperty(org.apache.axis2.transport.http.HTTPConstants.AUTHENTICATE, auth);
                options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
                options.setAction("http://XXXXX/XXX/RetrieveMultiple");
                options.setTo(new EndpointReference("http://XXXX/NZGP/XX/2011/XXX.svc/web"));
                serviceClient.setOptions(options);

                //Generate an OperationClient from the ServiceClient to execute the request
                OperationClient opClient = serviceClient.createClient(ServiceClient.ANON_OUT_IN_OP);

                //Have to translate MsgCtx from Synapse to Axis2
                org.apache.axis2.context.MessageContext axisMsgCtx = new org.apache.axis2.context.MessageContext();  
                axisMsgCtx.setEnvelope(context.getEnvelope());
                opClient.addMessageContext(axisMsgCtx);
                System.out.println("axisMsgCtx" + axisMsgCtx.toString());
                //Send the request to the server
                opClient.execute(true);

                //Retrieve Result and replace mediation (synapse) context
                SOAPEnvelope result = (SOAPEnvelope) opClient.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE).getEnvelope();
                context.setEnvelope((org.apache.axiom.soap.SOAPEnvelope) result);
System.out.println("result : "+result);
            } catch (AxisFault e) {

                context.setProperty("ResponseCode", e.getFaultCodeElement().getText());

                return false; //This stops the mediation flow, so I think it executes the fault sequence?

            }

            return true;


        }
/*
        public void setSoapAction(String _soapAction){
            soapAction = _soapAction;
        }

        public String getSoapAction(){
            return soapAction;
        }

        public void setSoapEndpoint(String _soapEndpoint){
            soapEndpoint = _soapEndpoint;
        }

        public String getSoapEndpoint(){
            return soapEndpoint;
        }

        public void setDomain(String _domain){
            domain = _domain;
        }

        public String getDomain(){
            return domain;
        }

        public void setHost(String _host){
            host = _host;
        }

        public String getHost(){
            return host;
        }

        public void setPort(int _port){
            port = _port;
        }

        public int getPort(){
            return port;
        }

        public void setUsername(String _username){
            username = _username;
        }

        public String getUsername(){
            return username;
        }

        public void setPassword(String _password){
            password = _password;
        }

        public String getPassword(){
            return password;
        }*/
}

0 个答案:

没有答案