转载自:
摘要其中关键点:(详细请参考上述原贴)
使用自定义拦截器,实现用户名与密码的检验
–服务器端的in拦截器
–客户端的out拦截器
–xiaoming/123456
客户端自定义拦截器
public class AddUserInterceptor extends AbstractPhaseInterceptor<SoapMessage>{
private String name;
private String password;
public AddUserInterceptor(String name,String password) {
super(Phase.PRE_PROTOCOL);//准备协议化时拦截
// TODO Auto-generated constructor stub
this.name = name;
this.password =password;
System.out.println("AddUserInterceptor ");
}
/*
<Envelope>
<head>
<authUser>
<name>xiaoming</name>
<password>123456</password>
</authUser>
<head>
<Body>
<sayHello>
<arg0>xiaoming</arg0>
<sayHello>
</Body>
</Envelope>
*/
@Override
public void handleMessage(SoapMessage message) throws Fault {
List<Header> headers = message.getHeaders();
/*<authUser>
<name>xiaoming</name>
<password>123456</password>
</authUser> */
Document document = DOMUtils.createDocument();
Element rootEle = document.createElement("authUser");
Element nameEle = document.createElement("name");
nameEle.setTextContent(name);
rootEle.appendChild(nameEle);
Element passwordEle = document.createElement("password");
passwordEle.setTextContent(password);
rootEle.appendChild(passwordEle);
headers.add(new Header(new QName("authUser"), rootEle));
System.out.println("client HandMessage()....");
}
}
客户端spring配置文件修改
<?xml version="1.0" encoding="UTF-8"?>
<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/jaxws">
<jaxws:client id="orderClient" serviceClass="com.me.cxf.ws.OrderWS"
address="http://localhost:8081/WS_CXF_spring_server/orderws">
<jaxws:outInterceptors>
<!-- 添加cxf日志出拦截器 -->
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
<!-- 添加自定义cxf拦截器 -->
<bean class="com.me.ws.cxf.Interceptor.AddUserInterceptor">
<constructor-arg index="0" value="xiaoming"></constructor-arg>
<constructor-arg index="1" value="123456"></constructor-arg>
</bean>
</jaxws:outInterceptors>
<jaxws:inInterceptors>
<!-- 添加cxf日志入拦截器 -->
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
</jaxws:inInterceptors>
</jaxws:client>
</beans>
服务端
自动义拦截器
public class CheckUserInterceptor extends AbstractPhaseInterceptor<SoapMessage>{
public CheckUserInterceptor() {
super(Phase.PRE_PROTOCOL);
System.out.println("CheckUserInterceptor() ");
// TODO Auto-generated constructor stub
}
/*
<Envelope>
<head>
<authUser>
<name>xiaoming</name>
<password>123456</password>
</authUser>
<head>
<Body>
<sayHello>
<arg0>xiaoming</arg0>
<sayHello>
</Body>
</Envelope>
*/
@Override
public void handleMessage(SoapMessage message) throws Fault {
Header header = message.getHeader(new QName("authUser"));
if(header!=null){
Element authUser = (Element) header.getObject();
String name = authUser.getElementsByTagName("name").item(0).getTextContent();
String password = authUser.getElementsByTagName("password").item(0).getTextContent();
if("xiaoming".equalsIgnoreCase(name) && "123456".equals(password)){
System.out.println("server 通过认证拦截器。。。");
return ;
}
}
//不能通过
System.out.println("server 没有通过认证拦截器。。。");
throw new Fault(new RuntimeException("请求输入一个正确的用户名密码"));
}
}
服务端spring配置文件修改
<?xml version="1.0" encoding="UTF-8"?>
<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/jaxws">
<!-- 引cxf的一些核心配置 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<!-- <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> -->
<jaxws:endpoint
id="orderWS"
implementor="com.me.cxf.ws.OrderWSImpl"
address="/orderws">
<!-- 添加入cxf拦截器 -->
<jaxws:inInterceptors>
<ref bean="loggingInInterceptor"/>
<ref bean="checkUserInterceptor"/>
</jaxws:inInterceptors>
<!-- 添加出cxf拦截器 -->
<jaxws:outInterceptors>
<ref bean="loggingOutInterceptor"/>
</jaxws:outInterceptors>
</jaxws:endpoint>
<!-- 自定义cxf拦截器 -->
<bean id="checkUserInterceptor" class="com.me.ws.cxf.interceptor.CheckUserInterceptor"/>
<!-- 添加cxf日志入拦截器 -->
<bean id="loggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
<!-- 添加cxf日志出拦截器 -->
<bean id="loggingOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
</beans>
浙公网安备 33010602011771号