我在使用CXF中的拦截器时遇到了麻烦。
我的项目如下所示
我的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.sun.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/root-config.xml</param-value>
</context-param>
<listener>
<display-name>contextLoaderListener</display-name>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
根-config.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:flex="http://www.springframework.org/schema/flex" xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:cxf="http://cxf.apache.org/core" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/flex
http://www.springframework.org/schema/flex/spring-flex-1.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<import resource="cxf-service.xml" />
</beans>
CXF-service.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://cxf.apache.org/transports/http/configuration
http://cxf.apache.org/schemas/configuration/http-conf.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="classpath*:META-INF/cxf/cxf.xml" />
<import resource="classpath*:META-INF/cxf/cxf-servlet.xml" />
<import resource="classpath*:META-INF/cxf/cxf-extension-http.xml" />
<import resource="classpath*:META-INF/cxf/cxf-extension-soap.xml" />
<jaxws:endpoint>
id="helloWorldService"
address="/helloworld">
<jaxws:implementor>
<bean id="HelloWorldImpl" class="com.jorge.test.hello.webservices.HelloWorldImpl">
</bean>
</jaxws:implementor>
<jaxws:inInterceptors>
<bean class="com.jorge.test.hello.interceptors.ValueCheckInterceptor"/>
</jaxws:inInterceptors>
</jaxws:endpoint>
</beans>
HelloWorld.java
package com.jorge.test.hello.webservices;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
public abstract String sayHello(@WebParam(name = "user") String user);
}
helloWorldImpl.java
package com.jorge.test.hello.webservices;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
@WebService(endpointInterface = "com.jorge.test.hello.webservices.HelloWorld", serviceName = "hello")
@BindingType("http://www.w3.org/2003/05/soap/bindings/HTTP/")
public class HelloWorldImpl implements HelloWorld {
public String sayHello(String user) {
if ((user == null) || (user.length() == 0)) {
return "hello world";
}
return "hello world " + user;
}
}
ValueCheckInterceptor.java
package com.jorge.test.hello.interceptors;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
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 ValueCheckInterceptor extends AbstractSoapInterceptor {
public ValueCheckInterceptor() {
super(Phase.RECEIVE);
System.out.println("ValueCheckInterceptor Instantiated");
}
@Override
public void handleMessage(SoapMessage message) throws Fault {
// TODO Auto-generated method stub
System.out.println("Intercepted at RECEIVE");
}
}
当我在JBOSS中编译和部署它时,我得到一个端点(基于HelloWorldImpl上的servicename注释) http://localhost:8080/helloworldws/hello 和另一个基于cxf(地址值) http://localhost:8080/helloworldws/services/helloworld
这两个端点都能够接收请求并对其进行处理。问题是,当我将请求发送到第一个端点时,Interceptor不会被调用,因为CXF不处理此端点,所以可以理解。当我向第二个端点发送请求时,会调用拦截器。
我尝试删除HellowWorldImpl上的servicename注释,但它只是在http://localhost:8080/helloworldws/HelloWorldImpl上公开了该服务。
似乎没有办法解决这个问题。如何防止未拦截的端点(http://localhost:8080/helloworldws/hello)暴露,以便消费者不向其发送请求?