- 浏览: 1380 次
- 性别:
- 来自: 广西
文章分类
最新评论
服务器端验证代码:
Spring业务验证:
applicationContext-xfire.xml
applicationContext-demo-xfire.xml
客户端验证代码:
applicationContext-demo-spring.xml
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>
相关推荐
xfire web service调用代码
xfire根据WSDL生成客户端代码.rar
xfire+spring+安全认证,包含服务端和客户端,xfire jar包可以从我的资源下面下或网上很多。
NULL 博文链接:https://dingqijie.iteye.com/blog/364404
xfire的安全策略,验证、签名、加密的详细做法,含有详细例子,看完后完全掌握xfire的ws-security实现方法
WebService 之XFire 开发文档和代码例子
xfire1.2.6 源代码 源文件 源程序。供调试、研究用。
xfire客户端生成自动生成java代码调用
xfire框架构建webservice应用: (通过myeclipse自动生成代码,自动导入xfire jar包,需要用jdk5.0) 需要的jar包:xFire 1.2 Core Libraries/xFire 1.2 HTTP Client Libraries/xFire 1.2 JAXB2 Libraries
xfire实现的实例和远程调用的客户端代码
Xfire API 对开发web services有帮助!编写程序时作为参考!
webservice Soap和XFire2种方式代码列子
NULL 博文链接:https://wander312.iteye.com/blog/1180698
数据同步服务端。采用xfire框架开发的webservice数据同步程序。
XFire--Webservice入门xfire
xfire-all-1.2.6 xfire所需的全部包
webservice服务,使用Xfire框架调用服务的常见客户端代码
xfire集成spring开发的一个数据库登陆验证实例,包含SSL安全机制
Xfire案例
xfire 1.26 博文链接:https://qgl.iteye.com/blog/119726