2014-07-02 110 views
8

嘗試使用maven使用cxf + wss4j。創建服務和客戶端沒有任何編譯問題。該服務在tomcat中運行良好。
問題: 當我運行的客戶端代碼,我得到「java.lang.NoSuchMethodError:org.apache.xml.security.utils.I18n.init(Ljava/UTIL /資源包;)V」。這個類是在xmlsec jar中,它附帶了cxf分發。cxf + wss4j + maven NoSuchMethod錯誤

pom.xml的服務項目:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>userNameTokenService</groupId> 
    <artifactId>userNameTokenService</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 
    <dependencies> 
     <dependency> 
      <groupId>org.apache.cxf</groupId> 
      <artifactId>cxf-rt-frontend-jaxws</artifactId> 
      <version>3.0.0</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.cxf</groupId> 
      <artifactId>cxf-rt-transports-http</artifactId> 
      <version>3.0.0</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.cxf</groupId> 
      <artifactId>cxf-rt-ws-security</artifactId> 
      <version>3.0.0</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.ws.security</groupId> 
      <artifactId>wss4j</artifactId> 
      <version>1.6.15</version> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
      <version>3.2.6.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
      <version>3.2.6.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-core</artifactId> 
      <version>3.2.6.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-beans</artifactId> 
      <version>3.2.6.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-expression</artifactId> 
      <version>3.2.6.RELEASE</version> 
     </dependency> 
    </dependencies> 
    <build> 
     <pluginManagement> 
      <plugins> 
       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-war-plugin</artifactId> 
        <configuration> 
         <webXml>WebContent\WEB-INF\web.xml</webXml> 
        </configuration> 
       </plugin> 
      </plugins> 
     </pluginManagement> 
    </build> 
</project> 


pom.xml的客戶端項目

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>userNameTokenClient</groupId> 
    <artifactId>userNameTokenClient</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <dependencies> 
     <dependency> 
      <groupId>org.apache.cxf</groupId> 
      <artifactId>cxf-rt-frontend-jaxws</artifactId> 
      <version>3.0.0</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.cxf</groupId> 
      <artifactId>cxf-rt-transports-http</artifactId> 
      <version>3.0.0</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.cxf</groupId> 
      <artifactId>cxf-rt-ws-security</artifactId> 
      <version>3.0.0</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.ws.security</groupId> 
      <artifactId>wss4j</artifactId> 
      <version>1.6.15</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
      <version>3.2.6.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-core</artifactId> 
      <version>3.2.6.RELEASE</version> 
     </dependency> 
    </dependencies> 
    <build> 
    <sourceDirectory>src</sourceDirectory> 
    <resources> 
     <resource> 
     <directory>src</directory> 
     <excludes> 
      <exclude>**/*.java</exclude> 
     </excludes> 
     </resource> 
    </resources> 
    <plugins> 
     <plugin> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <version>3.0</version> 
     <configuration> 
      <source>1.6</source> 
      <target>1.6</target> 
     </configuration> 
     </plugin> 
    </plugins> 
    </build> 
</project> 

編輯: 使用WSS4J來嘗試用戶名令牌,客戶端代碼:

public static void main(String[] args) { 
     ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml"); 
     HelloWorld helloworld= (HelloWorld) context.getBean("helloClient"); 
     HelloRequest hreq = new HelloRequest(); 
     hreq.setRequestMsg("This is client"); 
     HelloResponse hres = helloworld.sayHello(hreq); 
     System.out.println(hres.getResponseMsg()); 
    } 

客戶WSS4J配置:

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=" 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> 

    <bean id="logInBound" class="org.apache.cxf.interceptor.LoggingInInterceptor" /> 
    <bean id="logOutBound" class="org.apache.cxf.interceptor.LoggingOutInterceptor" /> 
    <jaxws:client id="helloClient" serviceClass="com.ddmwsst.helloworld.HelloWorld" 
     address="http://localhost:8080/userNameTokenService/services/HelloWorld"> 
     <jaxws:inInterceptors> 
      <ref bean="logInBound" /> 
     </jaxws:inInterceptors> 
     <jaxws:outInterceptors> 
      <ref bean="logOutBound" /> 
      <ref bean="outbound-security" /> 
     </jaxws:outInterceptors> 
    </jaxws:client> 

    <!-- WSS4JOutInterceptor for incorporating a UsernameToken in a SOAP --> 
    <bean class="org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor" 
     id="outbound-security"> 
     <constructor-arg> 
      <map> 
       <entry key="action" value="UsernameToken" /> 
       <entry key="user" value="dummy" /> 
       <!--entry key="passwordType" value="PasswordText"/ --> 
       <entry key="passwordCallbackClass" value="client.ClientPasswordCallback" /> 
      </map> 
     </constructor-arg> 
    </bean> 
</beans> 

服務WSS4J配置:

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" 
    xsi:schemaLocation=" 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> 

    <import resource="classpath:META-INF/cxf/cxf.xml" /> 
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> 

    <bean id="logInBound" class="org.apache.cxf.interceptor.LoggingInInterceptor" /> 
    <bean id="logOutBound" class="org.apache.cxf.interceptor.LoggingOutInterceptor" /> 
    <!-- WSS4JInInterceptor for processing a UsernameToken from the SOAP --> 
    <bean id="inbound-security" class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor"> 
     <constructor-arg> 
      <map> 
       <entry key="action" value="UsernameToken" /> 
       <!--entry key="passwordType" value="PasswordText"/ --> 
       <entry key="passwordCallbackClass" value="server.ServerPasswordCallback" /> 
      </map> 
     </constructor-arg> 
    </bean> 

    <jaxws:endpoint id="helloWorld" implementor="server.HelloWorldImpl" 
     address="/HelloWorld"> 
     <jaxws:inInterceptors> 
      <ref bean="logInBound" /> 
      <ref bean="inbound-security" /> 
     </jaxws:inInterceptors> 
     <jaxws:outInterceptors> 
      <ref bean="logOutBound" /> 
     </jaxws:outInterceptors> 
    </jaxws:endpoint> 
</beans> 

回答

8

CXF 3.0.0不WSS4J 1.6.15工作。您需要改用WSS4J 2.0.0。

Colm。

+0

沒有maven的嘗試 - 移回到cxf 2.7.7。從客戶端刪除xmlsec.jar並在服務器中使用xmlsec.jar 1.3.0。 &wss4j 1.6.15在客戶和服務,現在它的工作正常。 –

+2

WSS4J 2.x似乎不是maven中的jar工件。有一個zip文件,但使用正常的gradle/pom類型包括似乎目前不能獲取源代碼和在本地構建代碼。 WSS4J 2.x罐子是否在世界其他地方舉辦過? – Christian

+0

我也對W44S4J 2.x罐子感興趣......任何人? –

4

我也有同樣的問題。它通過使用依賴 「WSS4J,WS-Security的常見-2.0.3.jar」

更多細節上WSS4J 2.0.0遷移解決,PL請參閱 「http://ws.apache.org/wss4j/migration/wss4j20.html

+0

如何在IntelliJ項目中添加此依賴項? –

+0

https://mvnrepository.com/artifact/org.apache.wss4j/wss4j/2.1.7 – Nacho

+0

謝謝。我們已經使用 'org.apache.wss4j.common.ext.WSPasswordCallback pc =(org.apache.wss4j.common.ext.WSPasswordCallback)callbacks [0]; pc.setPassword(password);' 由於某些依賴性,我們不得不使用wss4j-1.6.19.jar。上面的類來自wss4j-ws-security-common-2.1.5.jar – Vins

0

當您添加WSS4J依賴性請務必將其添加爲POM類型,否則就會產生一個錯誤。這是前人的精力在你的POM的依賴

<dependency> 
     <groupId>org.apache.wss4j</groupId> 
     <artifactId>wss4j</artifactId> 
     <version>2.1.7</version> 
     <type>pom</type> 
    </dependency> 

還要確保指定maven的中央資料庫,以便它可以找到它肯定:

<repositories> 
     <repository> 
      <id>central</id> 
      <url>http://central.maven.org/maven2/</url> 
      <releases> 
       <enabled>true</enabled> 
      </releases> 
     </repository> 
    </repositories> 

完蛋了。