Xfire 验证,不想些文字,只想贴代码!~~

服务器端验证代码:

package com.shengshu.demo.xfire.authentication;

import org.apache.log4j.Logger;
import org.codehaus.xfire.MessageContext;
import org.codehaus.xfire.exchange.InMessage;
import org.codehaus.xfire.fault.XFireFault;
import org.codehaus.xfire.handler.AbstractHandler;
import org.jdom.Namespace;
import org.jdom.Element;

import com.shengshu.demo.xfire.bean.AuthenticationManagerService;

/**
*
* Jul 4, 2009
*
* @author PangShuqiang
* @blog http://pangshuqiang.blog.163.com/
* @Email pangshuqiang@163.com (or) pangsirsky@163.com
* @QQ 127015919 (or) 285204808
* @version 1.0
*
*/
public class AuthenticationHandler extends AbstractHandler {

private static final Logger log = Logger
.getLogger(AuthenticationHandler.class);

private AuthenticationManagerService authenticationManagerService;

protected static String namespaceValue = "";

protected final static String NAMESPACE = "WebserviceN";
protected final static String _HEADERS = "WebserviceH";
protected final static String USERNAME = "WebserviceU";
protected final static String PASSWORD = "WebserviceP";

public AuthenticationManagerService getAuthenticationManagerService() {
return authenticationManagerService;
}

public void setAuthenticationManagerService(
AuthenticationManagerService authenticationManagerService) {
this.authenticationManagerService = authenticationManagerService;
}

public void setNamespaceValue(String arg) {
namespaceValue = arg;
}

public void invoke(MessageContext context) throws Exception {
log.info("#AuthenticationHandler is invoked");
InMessage message = context.getInMessage();

final Namespace TOKEN_NS = Namespace.getNamespace(NAMESPACE, namespaceValue);

if (message.getHeader() == null) {
throw new XFireFault("GetRelation Service Should be Authenticated",
XFireFault.SENDER);
}

Element token = message.getHeader().getChild(_HEADERS, TOKEN_NS);
if (token == null) {
throw new XFireFault("Request must include authentication token.",
XFireFault.SENDER);
}

String username = token.getChild(USERNAME, TOKEN_NS).getValue();
String password = token.getChild(PASSWORD, TOKEN_NS).getValue();

System.out.println("username=" + username);
System.out.println("password=" + password);

if (username == null || password == null)
throw new XFireFault("Supplied Username and Password Please",
XFireFault.SENDER);

/**
*
* Spring 业务验证
*
*/
if (!authenticationManagerService.authenticate(username, password))
throw new XFireFault("Authentication Fail! Check username/password",
XFireFault.SENDER);
}
}



Spring业务验证:

package com.shengshu.demo.xfire.bean;

/**
*
* Jul 4, 2009
*
* @author PangShuqiang
* @blog http://pangshuqiang.blog.163.com/
* @Email pangshuqiang@163.com (or) pangsirsky@163.com
* @QQ 127015919 (or) 285204808
* @version 1.0
*
*/
public class AuthenticationManagerService {
private String username = "";
private String password = "";

public void setPassword(String password) {
this.password = password;
}

public void setUsername(String username) {
this.username = username;
}

/**
*
* @param checkUsername
* @param chekcPassword
* @return
*/
public boolean authenticate(String checkUsername, String chekcPassword) {
if(checkUsername == null || chekcPassword == null)
return false;

if(username == null || password == null)
return false;

if (username.equals(checkUsername) && password.equals(chekcPassword))
return true;

return false;
}
}



applicationContext-xfire.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
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-2.0.xsd">

<!-- 引入XFire预配置信息 -->
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />

<!-- 用户身份验证实例 -->
<bean id="authenticationManagerService" class="com.shengshu.demo.xfire.bean.AuthenticationManagerService">
<property name="username">
<value>庞树强</value>
</property>
<property name="password">
<value>南宁微庞信息技术有限公司</value>
</property>
</bean>

<!-- 使用XFire验证 -->
<bean id="authenticationHandler"
class="com.shengshu.demo.xfire.authentication.AuthenticationHandler">
<property name="authenticationManagerService">
<ref bean="authenticationManagerService" />
</property>
<property name="namespaceValue">
<value>http://service.webservice.plugins.flourishtree</value>
</property>
</bean>

<!-- 使用XFire导出器 -->
<bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter" lazy-init="false" abstract="true">
<!-- 引用xfire.xml中定义的工厂 -->
<property name="serviceFactory" ref="xfire.serviceFactory" />
<!-- 引用xfire.xml中的xfire实例 -->
<property name="xfire" ref="xfire" />
</bean>

<bean id="baseWebServiceMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" scope="prototype" lazy-init="false" abstract="true">
</bean>
</beans>


applicationContext-demo-xfire.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
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-2.0.xsd">

<!-- 定义所有访问 WEB 服务的 url -->
<bean parent="baseWebServiceMapping">
<property name="urlMap">
<map>
<entry key="/demoService">
<ref bean="demo"/>
</entry>
<entry key="/demoServiceMF">
<ref bean="demoMF"/>
</entry>
<entry key="/demoCommonService">
<ref bean="demoCommon"/>
</entry>
</map>
</property>
</bean>

<!-- Declare a parent bean with all properties common to both services -->
<bean id="demo" parent="baseWebService">
<!-- 业务服务bean -->
<property name="serviceBean">
<ref bean="demoImpl"/>
</property>
<!-- 业务服务bean的窄接口类 -->
<property name="serviceClass">
<value>com.shengshu.demo.xfire.service.IDemo</value>
</property>
<!-- 需要验证 -->
[b]<property name="inHandlers">
<list>
<ref bean="authenticationHandler" />
</list>
</property>[/b]
</bean>

<!-- 不需要验证 -->
<bean id="demoMF" parent="baseWebService">
<!-- 业务服务bean -->
<property name="serviceBean">
<ref bean="demoImpl"/>
</property>
<!-- 业务服务bean的窄接口类 -->
<property name="serviceClass">
<value>com.shengshu.demo.xfire.service.IDemoMF</value>
</property>
</bean>

<!-- 字符串和整数 -->
<bean id="demoCommon" parent="baseWebService">
<!-- 业务服务bean -->
<property name="serviceBean">
<ref bean="demoCommonImpl"/>
</property>
<!-- 业务服务bean的窄接口类 -->
<property name="serviceClass">
<value>com.shengshu.demo.xfire.service.IDemoCommon</value>
</property>
</bean>

</beans>




客户端验证代码:


package com.shengshu.demo.xfire.authentication;

import org.apache.log4j.Logger;
import org.codehaus.xfire.MessageContext;
import org.codehaus.xfire.handler.AbstractHandler;
import org.jdom.Element;
import org.jdom.Namespace;

/**
*
* Jul 4, 2009
*
* @author PangShuqiang
* @blog http://pangshuqiang.blog.163.com/
* @Email pangshuqiang@163.com (or) pangsirsky@163.com
* @QQ 127015919 (or) 285204808
* @version 1.0
*
*/
public class ClientAuthHandler extends AbstractHandler {
private static final Logger log = Logger.getLogger(ClientAuthHandler.class);

//客户端自己配置用户名密码或者更安全的KeyStore方式
private String username = "";
private String password = "";

protected static String namespaceValue = "";

protected final static String _HEADER = "header";
protected final static String NAMESPACE = "WebserviceN";
protected final static String _HEADERS = "WebserviceH";
protected final static String USERNAME = "WebserviceU";
protected final static String PASSWORD = "WebserviceP";

public ClientAuthHandler() {
}

public ClientAuthHandler(String username, String password) {
this.username = username;
this.password = password;
}

public void setUsername(String username) {
this.username = username;
}

public void setPassword(String password) {
this.password = password;
}

public void setNamespaceValue(String arg) {
namespaceValue = arg;
}

public void invoke(MessageContext context) throws Exception {
/*******************************************
* Soap Header方式
* 从Soap Header中获取用户名密码
*******************************************/
final Namespace ns = Namespace.getNamespace(NAMESPACE, namespaceValue);
Element el = new Element(_HEADER, ns);

Element auth = new Element(_HEADERS, ns);
Element username_el = new Element(USERNAME, ns);
username_el.addContent(username);
Element password_el = new Element(PASSWORD, ns);
password_el.addContent(password);
auth.addContent(username_el);
auth.addContent(password_el);
el.addContent(auth);
context.getCurrentMessage().setHeader(el);
log.info("ClientAuthHandler done!");
}
}



applicationContext-demo-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
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-2.0.xsd">

<!-- 需要验证 -->
<bean id="demoService"
class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean">
<property name="serviceClass">
<value>com.shengshu.demo.xfire.service.IDemo</value>
</property>
<property name="wsdlDocumentUrl">
<value>http://192.168.1.2:8080/x/demoService?WSDL</value>
</property>
[b]<property name="outHandlers">
<ref bean="clientAuthHandler" />
</property>[/b]
</bean>
<!-- 不需要验证 -->
<bean id="demoServiceMF"
class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean">
<property name="serviceClass">
<value>com.shengshu.demo.xfire.service.IDemo</value>
</property>
<property name="wsdlDocumentUrl">
<value>http://192.168.1.2:8080/x/demoServiceMF?WSDL</value>
</property>
</bean>

</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值