Spring WS 安全问题

Problems with Spring WS Security

我有一个 Spring Web 服务项目(spring 的示例:https://spring.io/guides/gs/producing-web-service/),我想设置安全性。好吧,当我添加 XwsSecurityInterceptor 时执行失败,因为编译器没有找到 class: com/sun/xml/wss/XWSSecurityException

下面,您可以找到我的 pom.xml、web 服务配置和错误跟踪:

POM.XML

  ...
  <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.3.5.RELEASE</version>

  <!-- Dependencia Proyecto Web de Spring Boot -->
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <!-- Dependencia para Web Services de Spring Boot -->
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-ws</artifactId>
  </dependency>

 <dependency>
    <groupId>org.springframework.ws</groupId>
    <artifactId>spring-ws-security</artifactId>
    <version>2.3.0.RELEASE</version>
</dependency>


  <dependency>
      <groupId>wsdl4j</groupId>
      <artifactId>wsdl4j</artifactId>
  </dependency>

  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
  </dependency>

  <dependency>
        <groupId>org.apache.ws.security</groupId>
        <artifactId>wss4j</artifactId>
        <version>1.6.19</version>
    </dependency>

...

@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {
    @Bean
    public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        servlet.setTransformWsdlLocations(true);
        return new ServletRegistrationBean(servlet, "/ws/*");
    }
@Bean(name = "countries")
public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema countriesSchema) {
    DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
    wsdl11Definition.setPortTypeName("CountriesPort");
    wsdl11Definition.setLocationUri("/ws");
    wsdl11Definition.setTargetNamespace("http://spring.io/guides/gs-producing-web-service");
    wsdl11Definition.setSchema(countriesSchema);
    return wsdl11Definition;
}

@Bean
public XsdSchema countriesSchema() {
    return new SimpleXsdSchema(new ClassPathResource("ws/countries.xsd"));
}


@Bean(name= "wsSecurityInterceptor")
public XwsSecurityInterceptor xwsSecurityInterceptor(){

    Resource securityPolicy = new ClassPathResource("ws/resources/SecurityPolicy.xml");

    XwsSecurityInterceptor xwsSI = new XwsSecurityInterceptor();

    xwsSI.setPolicyConfiguration(securityPolicy);
    xwsSI.setCallbackHandler(simplePasswordValidationCallbackHandler());

    return xwsSI;

}


@Bean(name = "passwordValidationHandler")
public SimplePasswordValidationCallbackHandler simplePasswordValidationCallbackHandler(){

    HashMap<String, String> user = new HashMap<String, String>();
    user.put("Bert", "Ernie");

    SimplePasswordValidationCallbackHandler spvch = new SimplePasswordValidationCallbackHandler();
    spvch.setUsersMap(user);

    return spvch;       
}

}

错误跟踪

Caused by: java.lang.ClassNotFoundException: com.sun.xml.wss.XWSSecurityException
    at java.net.URLClassLoader.run(Unknown Source) ~[na:1.8.0_20]
    at java.net.URLClassLoader.run(Unknown Source) ~[na:1.8.0_20]
    at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_20]
    at java.net.URLClassLoader.findClass(Unknown Source) ~[na:1.8.0_20]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_20]
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) ~[na:1.8.0_20]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_20]
    ... 30 common frames omitted

谢谢!

我已经解决了问题,我刚刚将下一个依赖添加到我的 pom 中:

<dependency>
        <groupId>com.sun.xml.wss</groupId>
        <artifactId>xws-security</artifactId>
        <version>3.0</version>
        <exclusions>
            <exclusion>
                <groupId>javax.xml.crypto</groupId>
                <artifactId>xmldsig</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

谢谢!