论坛首页 Java企业应用论坛

WebService soaphader 用户信息验证

浏览 4764 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2014-05-09   最后修改:2014-05-12
WebService 高手帮吗?

现在在弄一个WebService的客户端,用java来写。
已经用wsimport生成好了客户端的代码,wsdl是一http来请求的,不是https

发送request请求的时候需要用户验证,
soapheader代码如下:

package com.webservice.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.xml.namespace.QName;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.Handler;
import javax.xml.ws.handler.HandlerResolver;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.PortInfo;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;

import org.apache.log4j.Logger;

class HeaderHandler implements SOAPHandler<SOAPMessageContext>{

	private static final String USER_NAME = "my";
	private static final String PASSWORD = "123456";
	private Logger log = Logger.getLogger(this.getClass());
	
	@Override
	public boolean handleMessage(SOAPMessageContext smc) {
		Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

		if (outboundProperty.booleanValue()) {
			SOAPMessage message = smc.getMessage();
			log.info("HeaderHandler outboundProperty="+outboundProperty+","+message);
			try {
				SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
				SOAPHeader header = envelope.addHeader();
				/*
				SOAPElement security =header.addChildElement("Security", "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
		
				SOAPElement usernameToken =security.addChildElement("UsernameToken", "wsse");
				usernameToken.addAttribute(new QName("xmlns:wsu"), "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
		
				SOAPElement username =usernameToken.addChildElement("Username", "wsse");
				username.addTextNode(USER_NAME);
		
				SOAPElement password =usernameToken.addChildElement("Password", "wsse");
				password.setAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
				password.addTextNode(PASSWORD);
				*/
				SOAPElement h = header.addChildElement("Header","soapenv","");
				SOAPElement authenticationToken = h.addChildElement("UsernameToken","pws","");
				authenticationToken.addChildElement("Username","pws").addTextNode(USER_NAME);
				authenticationToken.addChildElement("Password","pws").addTextNode(PASSWORD);
				message.writeTo(System.out);
				System.out.println("=======================over1================================");
			} catch (Exception e) {
				log.error("HeaderHandler.handleMessage:Error,outboundProperty="+outboundProperty,e);
			}
		} else {
			try {
			//This handler does nothing with the response from the Web Service so
			//we just print out the SOAP message.
			SOAPMessage message = smc.getMessage();
			message.writeTo(System.out);
			System.out.println("=======================over2================================");
			log.info("HeaderHandler outboundProperty="+outboundProperty+","+message);
			} catch (Exception ex) {
				log.error("HeaderHandler.handleMessage:Error,outboundProperty="+outboundProperty,ex);
			} 
		}
		return outboundProperty;
	}

	@Override
	public boolean handleFault(SOAPMessageContext context) {
		return true;
	}

	@Override
	public void close(MessageContext context) {
		
	}

	@Override
	public Set<QName> getHeaders() {
		return null;
	}
	
}

class HeaderHandlerResolver implements HandlerResolver {

	@Override
	public List<Handler> getHandlerChain(PortInfo portInfo) {
		List<Handler> handlerChain = new ArrayList<Handler>();
		HeaderHandler hh = new HeaderHandler();
		handlerChain.add(hh);
		return handlerChain;
	}
	
}



一直发生这个错误:
org.apache.cxf.phase.PhaseInterceptorChain] Interceptor for {http://servicecenter.peregrine.com/PWS}CMBOndutyPlan#{http://servicecenter.peregrine.com/PWS}RetrieveCMBOndutyPlanList has thrown exception, unwinding now: org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted.
at org.apache.axiom.om.impl.dom.ParentNode.insertBefore(ParentNode.java:235) [:1.2.13]
at org.apache.axiom.om.impl.dom.ParentNode.insertBefore(ParentNode.java:205) [:1.2.13]
at org.apache.axiom.om.impl.dom.NodeImpl.appendChild(NodeImpl.java:199) [:1.2.13]
at org.apache.axis2.saaj.SOAPPartImpl.appendChild(SOAPPartImpl.java:948) [:1.6.2]
at org.apache.cxf.staxutils.W3CDOMStreamWriter.setChild(W3CDOMStreamWriter.java:114) [:2.3.1]
at org.apache.cxf.staxutils.W3CDOMStreamWriter.newChild(W3CDOMStreamWriter.java:104) [:2.3.1]
at org.apache.cxf.staxutils.W3CDOMStreamWriter.writeStartElement(W3CDOMStreamWriter.java:132) [:2.3.1]
at org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor.writeSoapEnvelopeStart(SoapOutInterceptor.java:122) [:2.3.1]
at org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor.handleMessage(SoapOutInterceptor.java:81) [:2.3.1]
at org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor.handleMessage(SoapOutInterceptor.java:61) [:2.3.1]
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:255) [:2.3.1]
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:516) [:2.3.1]
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:313) [:2.3.1]
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:265) [:2.3.1]
at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:73) [:2.3.1]
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:124) [:2.3.1]
at $Proxy337.retrieveCMBOndutyPlanList(Unknown Source) at com.eccom.neteagle.server.webservice.service.CMBOndutyPlanService.start(CMBOndutyPlanService.java:83) [:]
at com.eccom.neteagle.common.service.impl.CommonJob.execute(CommonJob.java:56) [:]
at org.quartz.core.JobRunShell.run(JobRunShell.java:202) [:1.6.0]
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:529) [:1.6.0]

请教大家这是什么原因,头文件是什么错误?
   发表时间:2014-05-11  
  authenticationToken.addChildElement("Username","pws").addTextNode(USER_NAME); 
                authenticationToken.addChildElement("Password","pws").addTextNode(PASSWORD); 


改成

  authenticationToken.addChildElement("Username","pws")..setValue(USER_NAME);
0 请登录后投票
   发表时间:2014-05-12   最后修改:2014-05-12
首先谢谢 2楼 cbq881215 回复
我是这样改的,还是有些问题,还是报org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted.

public boolean handleMessage(SOAPMessageContext smc) {
		SOAPMessage message = smc.getMessage();
		log.info("HeaderHandler outboundProperty="+true+","+message);
		try {
			SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
			SOAPHeader header = envelope.addHeader();
			SOAPElement security =header.addChildElement("Security", "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
	
			SOAPElement usernameToken =security.addChildElement("UsernameToken", "wsse");
			usernameToken.addAttribute(new QName("xmlns:wsu"), "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
	
			SOAPElement username =usernameToken.addChildElement("Username", "wsse");
			username.setValue(USER_NAME);
	
			SOAPElement password =usernameToken.addChildElement("Password", "wsse");
			password.setAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
			password.setValue(PASSWORD);
			System.out.println("=======================over1================================");
		} catch (Exception e) {
			log.error("HeaderHandler.handleMessage:Error,outboundProperty="+true,e);
		}
		return true;
	}
0 请登录后投票
   发表时间:2014-09-03   最后修改:2014-09-03
server端cxf+spring配置,在srping配置内,增加了拦截器,client如何通过wsdl看出来使用拦截器
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics