cxf和axis2整合项目从jboss迁至weblogic

   项目开发初期用的中间件服务器是jboss,在jboss下面运行的很好。需求变更,上面要求更换中间件服务器。开始认为这是个简单的事情,就是一个部署问题。直到将项目在weblogic部署运行的一瞬间,彻底傻眼了。一大堆的错误。

项目的主体框架是ssh,在加上cxf:2.7.8,axis:1.6.2,quazr框架。
虽然问题解决了,但不得不吐槽一句,每一个中间件运行机制都不一样,所带的jar,也不样。是想累死我们这些程序猿。能不能统一下标准。

遇到的问题太多,都不清,也没记录下来。就把解决问题配置和代码贴出来。

web.xml
需要注意的地方,在jboss,tomcat下面,项目在运行的过程会解包,就不会存在路径找不到问题,而weblogic就会。典型的就是org.springframework.web.util.Log4jConfigListener。日志路径问题。网上有很多方法,有让直接servlet代替,都不靠谱。这是我查看Log4jConfigListener源码找到的方法。绝对的有效。提醒一下,如果有什么配置文件最好是放在classses路径下面。

<context-param>
  <param-name>log4jConfigLocation</param-name>
  <param-value>classpath:log4j.xml</param-value>
 </context-param>
 
 <!-- 是否暴露WebAppRoot weblogic需要此设置-->
<context-param>
	<param-name>log4jExposeWebAppRoot</param-name>
	<param-value>false</param-value>
</context-param>

 <!-- 指定webAppRootKey weblogic需要此设置-->
 <context-param>
	<param-name>webAppRootKey</param-name>
	<param-value>mywebApp.root</param-value>
</context-param>
<!-- 配置spring的监听器 -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:spring/applicationContext.xml</param-value>
 </context-param>
 
 <!-- log4j配置 --> 
  <listener>
  <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>


cxf和axis2

cxf和axi2在整合的过程中,会存在冲突,最主要是jar文件
cxf
org.apache.ws.xmlschema:xmlschema-core:jar:2.0.3:compile
axis2
删除
org.apache.ws.commons.schema:XmlSchema:jar:1.4.7:compile
stax:stax-api:jar:1.0.1:compile

cxf在用到wssj安全认证的时候,会遇到错误,需要设置相应的环境变量
cxf 客户端测试webserice
public static <T> T  getWsSecurityServivce(Class<T> servivceclass,String username,String password,String url,Boolean openInterceptorlog){
			
		T t=null;

		if(!"com.sun.xml.internal.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl".equals(System.getProperty("org.apache.cxf.binding.soap.messageFactoryClassName"))){
			
			System.setProperty("org.apache.cxf.binding.soap.messageFactoryClassName","com.sun.xml.internal.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl");
		}
		
		
		Map<String,Object> outProps = new HashMap<String,Object>();
		outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
		outProps.put(WSHandlerConstants.USER, username);//这个用户即服务器端配置的用户名
		outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
		//这个类用来获取客户端认证用户名密码信息
		outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, ClientAutherHandler.class.getName());
		//outProps.put(WSHandlerConstants.MUST_UNDERSTAND, "0");
		  
		if(openInterceptorlog){
		
			jaxWsProxyFactoryBean.getOutInterceptors().add(new LoggingOutInterceptor());  
			jaxWsProxyFactoryBean.getOutFaultInterceptors().add(new LoggingOutInterceptor()); 
			
		}
		
		jaxWsProxyFactoryBean.getOutInterceptors().add(new SAAJOutInterceptor());
		//密码验证拦截器 必须放置create方法前
		jaxWsProxyFactoryBean.getOutInterceptors().add(new WSS4JOutInterceptor(outProps));
	    jaxWsProxyFactoryBean.setServiceClass(servivceclass);
	    jaxWsProxyFactoryBean.setAddress(url); 
	    t = jaxWsProxyFactoryBean.create(servivceclass);
			
	    return t;
		}



ClientAutherHandler
package com.telecommunications.util;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;

import org.apache.log4j.Logger;
import org.apache.ws.security.WSPasswordCallback;

/**
 * 客户端用户名账号和密码验证
 * @author Administrator
 *
 */
public class ClientAutherHandler implements CallbackHandler{
	public static Logger logger=Logger.getLogger(ClientAutherHandler.class);
	
	private Map<String,String> users;
	
	public Map<String, String> getUsers() {
		return users;
	}

	public void setUsers(Map<String, String> users) {
		this.users = users;
	}

	public void handle(Callback[] callbacks) throws IOException,
			UnsupportedCallbackException {
		
		WSPasswordCallback  pc=(WSPasswordCallback) callbacks[0];
		String username=pc.getIdentifier();
		//System.out.println("------username----"+username);
		if(null!=users){
			Set<String> keyusers=users.keySet();
			for(String key:keyusers){
				if(key.equals(username)){
					pc.setIdentifier(username);
					pc.setPassword(users.get(key));
					break;
				}
			}
			
		}else{
			
			String password=WsPropertiesUtil.properties.getProperty(username);
			if(null==password||"".equals(password)){
				logger.warn(" ws-Security安全认证"+username+"密码未空");
			}
			pc.setIdentifier(username);
			pc.setPassword(password);
	}

	}

}




如果你的wssj客户端和spring继承了你就配置拦截器。

cxf 服务端
spring配置
 <jaxws:server id="pWsSicfrevenueauditoutsynService" 
        serviceClass="com.telecommunications.ws.serviceimp.WsSicfrevenueauditoutsynServiceImp"
        address="/pWsSicfrevenueauditoutsynServiceImp">
  
        
        <!--发布服务的bean  -->
        <jaxws:serviceBean>
        <bean class="com.telecommunications.ws.serviceimp.WsSicfrevenueauditoutsynServiceImp"></bean>
            <!-- 要暴露的 bean 的引用 -->
        </jaxws:serviceBean>
            <jaxws:inInterceptors> 
            <ref bean="loggingInInterceptor"/>
            <bean class="com.telecommunications.interceptor.CxfWssJSystemEnvIntercepter"></bean>
                 
            	 <bean class="org.apache.cxf.binding.soap.saaj.SAAJInInterceptor" />  
            	 <bean class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor">  
                 <constructor-arg>  
                    <map>  
                        <entry key="action" value="UsernameToken" />  
                        <entry key="passwordType"  
                            value="PasswordText" />  
                        <entry key="user" value="EDA" />  
                        <entry key="passwordCallbackRef">  
                            <ref bean="serverAutherHandler" />  
                        </entry>  
                    </map>  
                </constructor-arg>  
            </bean> 
          
           
          </jaxws:inInterceptors> 
       <jaxws:outInterceptors>
       		<ref bean="loggingOutInterceptor"/>
       </jaxws:outInterceptors>
    </jaxws:server>


CxfWssJSystemEnvIntercepter类
package com.telecommunications.interceptor;

import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;

public class CxfWssJSystemEnvIntercepter extends AbstractPhaseInterceptor<Message>{


	public CxfWssJSystemEnvIntercepter(String phase) {
			super(phase);
	}
	
	public CxfWssJSystemEnvIntercepter(){
		super(Phase.RECEIVE);
	}
		
	@Override
	public void handleMessage(Message message) throws Fault {
		
		if(null==System.getProperty("org.apache.cxf.binding.soap.messageFactoryClassName")||"".equals(System.getProperty("org.apache.cxf.binding.soap.messageFactoryClassName"))){
			
			System.setProperty("org.apache.cxf.binding.soap.messageFactoryClassName","com.sun.xml.internal.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl");
//linux 环境下需要做此设置 要不按Woodstox 4.2.0 有点问题
			System.setProperty("org.apache.cxf.stax.allowInsecureParser","1");    
			System.setProperty("org.apache.cxf.stax.maxChildElements","50000");
			System.setProperty("org.apache.cxf.stax.maxElementDepth","100");
			System.setProperty("org.apache.cxf.stax.maxAttributeCount","500");
			System.setProperty("org.apache.cxf.stax.maxAttributeSize",String.valueOf(64 * 1024));
			System.setProperty("org.apache.cxf.stax.maxTextLength",String.valueOf(128 * 1024 * 1024));
			System.setProperty("org.apache.cxf.stax.maxElementCount",String.valueOf(Long.MAX_VALUE));
			System.setProperty("org.apache.cxf.stax.maxXMLCharacters",String.valueOf(Long.MAX_VALUE));
			System.setProperty("org.apache.cxf.staxutils.innerElementCountThreshold",String.valueOf(50000));
			System.setProperty("org.apache.cxf.staxutils.innerElementLevelThreshold",String.valueOf(100));
		}
		
		
	}
	

}


serverAutherHandler
/**
 * 用户名账号和密码验证
 * @author Administrator
 *
 */
public class ServerAutherHandler implements CallbackHandler{
	public static Logger logger=Logger.getLogger(ServerAutherHandler.class);
	
	private Map<String,String> users;
	
	public Map<String, String> getUsers() {
		return users;
	}

	public void setUsers(Map<String, String> users) {
		this.users = users;
	}

	public void handle(Callback[] callbacks) throws IOException,
			UnsupportedCallbackException {
		
    
    
		WSPasswordCallback  pc=(WSPasswordCallback) callbacks[0];
		String username=pc.getIdentifier();
		//String password=pc.getPassword();
		
		//System.out.println("username"+username);
		//System.out.println("password"+password);
		
		if(null==users){
			logger.info("----未设置服务端验证账号和密码--------");
		}else{
			Set<String> keyusers=users.keySet();
			for(String key:keyusers){
				if(key.equals(username)){
					
					pc.setPassword(users.get(key));
					
					
					break;
				}
				
			}
		}
		
		
		
	}

}

hibernate
ClassNotFoundException: org.hibernate.hql.ast.HqlToken

1 把项目下 jar  antlr:antlr:jar:2.7.6 复制到自建域lib文件夹下面(看自己喜欢)
D:\weblogicinstall\user_projects\domains\myuser_domain\lib
2 修改 setDomainEnv.cmd文件
D:\weblogicinstall\user_projects\domains\myuser_domain\bin
set PRE_CLASSPATH=D:\weblogicinstall\user_projects\domains\myuser_domain\lib\antlr-2.7.6.jar
set CLASSPATH=%PRE_CLASSPATH%;%CLASSPATH%


weblogic
weblogic自带有webserive相关的jar.所以部署会和项目相关的jar冲突,还有就是jar加载先后顺序的问题。这是个头疼的事情。会造成一系列的问题。
在WEB-INF新建一个weblogic.xml
<?xml version="1.0" encoding="UTF-8"?>
<weblogic-web-app>  
<context-root>/mywebname</context-root>
  <container-descriptor>  
    <prefer-web-inf-classes>false</prefer-web-inf-classes>  
    
    <prefer-application-resources>  
        <resource-name>META-INF/services/javax.xml.ws.spi.Provider</resource-name>  
    </prefer-application-resources>  
    <prefer-application-packages>  
	     <package-name>org.python.core.*</package-name> 
		 <package-name>com.ctc.wstx.*</package-name>
    </prefer-application-packages>  
    </container-descriptor>  
 </weblogic-web-app> 


希望犯过的错误对各位有帮助,转载请说明出处
-----------------------------------------------------------------
现在项目遇到一个问题linux环境下会有警告信息:WARN : org.apache.cxf.staxutils.StaxUtils - Could not create a secure Stax XMLInputFactory.  Found class com.ctc.wstx.stax.WstxInputFactory.  Suggest Woodstox 4.2.0 or newer.不影响正常使用
在window环境下是没有警告信息。翻阅了源码,不知道如何去解决。有遇到且解决的朋友麻烦告知一声


你可能感兴趣的:(weblogic,ssh,cxf和axis整合,webservic)