axis1.4 调用CXF 服务器端报错(cxf 整合ssh)

发布于 2021-11-23 05:36:18 字数 9090 浏览 731 评论 1

spring整合CXF


AxisFault
 faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server
 faultSubcode: 
 faultString: Cannot create a secure XMLInputFactory
 faultActor: 
 faultNode: 
 faultDetail: 
{http://xml.apache.org/axis/}stackTrace:Cannot create a secure XMLInputFactory
at org.apache.axis.message.SOAPFaultBuilder.createFault(SOAPFaultBuilder.java:222)
at org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.java:129)
at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:601)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2939)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:647)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:395)
at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)
at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)
at org.apache.axis.Message.getSOAPEnvelope(Message.java:435)
at org.apache.axis.transport.http.HTTPSender.readFromSocket(HTTPSender.java:796)
at org.apache.axis.transport.http.HTTPSender.invoke(HTTPSender.java:144)
at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)
at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)
at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)
at org.apache.axis.client.AxisClient.invoke(AxisClient.java:165)
at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
at org.apache.axis.client.Call.invoke(Call.java:2767)
at org.apache.axis.client.Call.invoke(Call.java:2443)
at org.apache.axis.client.Call.invoke(Call.java:2366)
at org.apache.axis.client.Call.invoke(Call.java:1812)
at com.gdcct.ctp.base.Client.authentication(Client.java:50)

at com.gdcct.ctp.base.Client.main(Client.java:11)



// 安全验证
public class AuthenticationInterceptor extends AbstractPhaseInterceptor<SoapMessage>{
private String username;
private String password;

public AuthenticationInterceptor() {
// 指定该拦截器在哪个阶段被激发
super(Phase.PRE_INVOKE);
}

public void handleMessage(SoapMessage msg) throws Fault {
List<Header> headers = msg.getHeaders();
if (null == headers || headers.size() < 1) {
throw new Fault(new SOAPException("SOAPHeader格式错误"));
}
for (Header header : headers) {
Node root = (Node) header.getObject();
if("Authentication".equals(root.getNodeName())){
Node userNode = root.getFirstChild();
Node passwordNode = root.getLastChild();
if(userNode == null || passwordNode == null){
throw new Fault(new SOAPException("SOAPHeader需要包含Username和Password节点"));
}
if(!userNode.getNodeName().equals("Username") || !passwordNode.getNodeName().equals("Password")){
throw new Fault(new SOAPException("SOAPHeader需要包含Username和Password节点"));
}
if(username.equals(userNode.getTextContent()) && password.equals(passwordNode.getTextContent())){
System.out.println("认证通过");
}
else{
throw new Fault(new SOAPException("用户名或密码错误"));
}
}
else{
throw new Fault(new SOAPException("SOAPHeader需要包含Authentication节点"));
}
}
}



public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

}



server服务端

@WebService(targetNamespace="http://localhost:8090/api/HelloWorld")
public class HelloWorld {

public String hello(@WebParam(name="uname") String uname){
return "hello" + uname;
}
}


cxf 配置文件

 <!--导入与CXF框架有关的xml-->
        <import resource="classpath:META-INF/cxf/cxf.xml" />  
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
  
        <!--WebService接口--> 
    <jaxws:endpoint id="service"
      implementor="com.apis.HelloWorld" address="/HelloWorld">
      <jaxws:inInterceptors>    
      <bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />  
        <bean class="org.apache.cxf.binding.soap.saaj.SAAJInInterceptor" />
        
            <bean class="com.apis.security.AuthenticationInterceptor">  
                <property name="username" value="admin" />  
                <property name="password" value="admin" />  
            </bean>  
        </jaxws:inInterceptors>
    </jaxws:endpoint>



axis 客户端调用

public static void authentication(String user, String password){
try {
Call call = (Call)new Service().createCall();
String namespace = "";//命名空间
SOAPHeaderElement header = new SOAPHeaderElement(namespace,"Authentication");
header.setPrefix("");//前缀
        header.addChildElement("Username").addTextNode(user);  
        header.addChildElement("Password").addTextNode(password);
        call.addHeader(header);
        
        
        call.setTargetEndpointAddress("http://localhost:8090/api/HelloWorld");
        call.setOperationName(new QName("http://localhost:8090/api/HelloWorld","hello"));
String obj = (String) call.invoke(new Object[] {java.net.URLEncoder.encode("lihao", "UTF-8") });
System.out.println(obj);
} catch (Exception e) {
e.printStackTrace();

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天涯离梦残月幽梦 2021-11-26 14:56:45

少年,找到原因了?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文