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

Java org.apache.axis2.AxisFault:传输错误:401错误:从WSO2 ESB调用NTLM服务时未经授权,java,wso2,wso2esb,ntlm,Java,Wso2,Wso2esb,Ntlm,我正在尝试从WSO2 ESB调用NTLM服务。我已经为NTLM访问创建了一个自定义中介,但是每次都会得到“org.apache.axis2.AxisFault:Transport error:401 error:Unauthorized”,非常感谢您的帮助 代码: 包mbie.poc; 导入java.util.ArrayList; 导入javax.xml.soap.SOAPEnvelope; 导入org.apache.axis2.AxisFault; 导入org.apache.axis2.Con

我正在尝试从WSO2 ESB调用NTLM服务。我已经为NTLM访问创建了一个自定义中介,但是每次都会得到“org.apache.axis2.AxisFault:Transport error:401 error:Unauthorized”,非常感谢您的帮助

代码:

包mbie.poc;
导入java.util.ArrayList;
导入javax.xml.soap.SOAPEnvelope;
导入org.apache.axis2.AxisFault;
导入org.apache.axis2.Constants;
导入org.apache.axis2.addressing.EndpointReference;
导入org.apache.axis2.client.OperationClient;
导入org.apache.axis2.client.Options;
导入org.apache.axis2.client.ServiceClient;
导入org.apache.axis2.transport.http.HttpTransportProperties;
导入org.apache.axis2.wsdl.WSDLConstants;
导入org.apache.commons.httpclient.auth.AuthPolicy;
导入org.apache.synapse.MessageContext;
导入org.apache.synapse.mediators.AbstractMediator;
公共类NTLMAuthorization扩展了AbstractMediator{
/*私有字符串操作;
私有字符串端点;
私有字符串域;
私有字符串主机;
专用int端口;
私有字符串用户名;
私有字符串密码;
*/
公共布尔中介(MessageContext上下文){
//构建NTLM身份验证方案
System.out.println(“类代码执行…”);
//registerAuthScheme(AuthPolicy.NTLM,JCIFS\u NTLMScheme.class);
HttpTransportProperties.Authenticator auth=新的HttpTransportProperties.Authenticator();
授权设置用户名(“XXXX”);
验证设置密码(“P@XX");
认证设置域(“XX”);
授权设置主机(“XXXX”);
认证设置端口(80);
auth.setPreemptiveAuthentication(true);
ArrayList authPrefs=新的ArrayList();
添加(AuthPolicy.NTLM);
auth.setAuthSchemes(authPrefs);
System.out.println(“auth.NTLM:+authPrefs”);
//强制身份验证-失败将被捕获到catch块中
试一试{
//构建ServiceClient并设置授权选项
ServiceClient ServiceClient=新ServiceClient();
选项=新选项();
options.setProperty(org.apache.axis2.transport.http.HTTPConstants.AUTHENTICATE,auth);
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
选项。设置操作(“http://XXXXX/XXX/RetrieveMultiple");
options.setTo(新端点引用(“http://XXXX/NZGP/XX/2011/XXX.svc/web"));
serviceClient.setOptions(选项);
//从ServiceClient生成OperationClient以执行请求
OperationClient opClient=serviceClient.createClient(serviceClient.ANON\u OUT\u IN\u OP);
//必须将MsgCtx从Synapse转换为Axis2
org.apache.axis2.context.MessageContext axisMsgCtx=new org.apache.axis2.context.MessageContext();
axisMsgCtx.setEnvelope(context.getEnvelope());
addMessageContext(axisMsgCtx);
System.out.println(“axisMsgCtx”+axisMsgCtx.toString());
//将请求发送到服务器
opClient.execute(true);
//检索结果并替换中介(synapse)上下文
SOAPEnvelope结果=(SOAPEnvelope)opClient.getMessageContext(WSDLConstants.MESSAGE_标签_IN_值).getEnvelope();
setEnvelope((org.apache.axiom.soap.SOAPEnvelope)结果);
System.out.println(“结果:+result”);
}捕捉(Axise故障){
setProperty(“ResponseCode”,例如getFaultCodeElement().getText());
return false;//这会停止中介流,所以我认为它会执行错误序列?
}
返回true;
}
/*
公共void setSoapAction(字符串_soapAction){
soapAction=\u soapAction;
}
公共字符串getSoapAction(){
返回操作;
}
公共void setSoapEndpoint(字符串_soapEndpoint){
soapEndpoint=\u soapEndpoint;
}
公共字符串getSoapEndpoint(){
返回soapEndpoint;
}
public void setDomain(字符串_域){
域=_域;
}
公共字符串getDomain(){
返回域;
}
公共void setHost(字符串_host){
主机=_主机;
}
公共字符串getHost(){
返回主机;
}
公共无效设置端口(int\U端口){
端口=_端口;
}
公共int getPort(){
返回端口;
}
public void setUsername(字符串\u用户名){
用户名=_用户名;
}
公共字符串getUsername(){
返回用户名;
}
public void setPassword(字符串\u密码){
密码=_密码;
}
公共字符串getPassword(){
返回密码;
}*/
}

一个非常好的问题在作为中介编写之前,您是否能够让此代码作为独立的Java代码工作?401表示由于凭据不正确,身份验证无法工作。您好,Maheeka,是的,我已经创建了一个自定义中介来验证NTLM v2,我也将在此处放置代码。ThanksI已经在我的博客Spot上添加了一个关于NTLM安全性的博客一个非常好的问题在作为中介编写之前,您是否能够将此代码作为独立的Java代码工作?401表示由于凭据不正确,身份验证无法工作。您好,Maheeka,是的,我已经创建了一个自定义中介来验证NTLM v2,我也将在此处放置代码。ThanksI在我的博客上添加了一个关于NTLM安全的博客
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;
        }*/
}