Xfire采用了WSS4J作为Web Services Security (WS-Security)的实现,WSS4J是一java的jar包,它可以用来在发布服务时标记访问密码和在调用时验证密码。这个包的下载地址为http://www.apache.org/dyn/closer.cgi/ws/wss4j/,
在使用WS-Security之前我们需要安装一个jdk的加密处理包(Unlimited Strength Jurisdiction Policy Files),下载地址为http://java.sun.com/j2se/1.5.0/download.jsp(不太显眼,在下面),下载后的文件名为jce_policy-1_5_0.zip,解压后里面有两个jar文件,分别为local_policy.jar和US_export_policy.jar,把这两个文件放到java_home/jre/lib/security目录下覆盖原来的文件。还需要安装一个bcprov-jdk15-141.jar,下载地址为http://BouncyCastle.org,下载后放到java_home/jre/lib/ext目录下。这两个包都是必须的。
好了,以上是准备工作,下面我们就开为服务设置访问权限吧!
1.先写一个类叫PasswordHandler,代码如下
package org.codehaus.xfire.demo;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import org.apache.ws.security.WSPasswordCallback;
public class PasswordHandler implements CallbackHandler {
private Map passwords = new HashMap();
public PasswordHandler() {
passwords.put("user1", "pwd1");
passwords.put("user2", "pwd2");
}
public void handle(Callback[] callbacks) throws IOException,
UnsupportedCallbackException {
WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
String id = pc.getIdentifer();
pc.setPassword((String) passwords.get(id));
}
}
其中user1,user2表示可以提供多个用户名密码,调用时任选一个。
2.在services.xml配置中加入如下代码:
<service>
<name>BookServiceUTHP</name>
<namespace>http://xfire.codehaus.org/BookService</namespace>
<serviceClass>example.webservice.service.BookService</serviceClass>
<inHandlers>
<handler handlerClass="org.codehaus.xfire.util.dom.DOMInHandler" />
<bean class="org.codehaus.xfire.security.wss4j.WSS4JInHandler" xmlns="">
<property name="properties">
<props>
<prop key="action">UsernameToken</prop>
<prop key="passwordCallbackClass">
org.codehaus.xfire.demoPasswordHandler
</prop>
</props>
</property>
</bean>
</inHandlers>
</service>
其中红色部分的org.codehaus.xfire.demoPasswordHandler就是我们刚才上面写的那个类,这样别人在调服务的时候就会调用那个类来验证密码,密码的传输的过程会使用安装的那两个包进行加密和解密。OK,发布完成了。
3.服务的调用,写一个main方法下面加入
Service serviceModel = new ObjectServiceFactory().create(IBook.class);//接口类
Object service = new XFireProxyFactory().create(serviceModel, getUrl()
+ getServiceName());
Client client = ((XFireProxy) Proxy.getInvocationHandler(service)).getClient();
client.addOutHandler(new DOMOutHandler());
Properties properties = new Properties();
properties.setProperty(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
properties.setProperty(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_DIGEST);
client.addOutHandler(new WSS4JOutHandler(properties));
client.setProperty(WSHandlerConstants.USER,"user1");
client.setProperty(WSHandlerConstants.PW_CALLBACK_REF,new PasswordHandler("pwd1"));//PasswordHandler是客户端的处理密码的类,代码如下
import java.io.IOException;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import org.apache.ws.security.WSPasswordCallback;
public class PasswordHandler implements CallbackHandler {
String password ;
public PasswordHandler(String password) {
this.password = password;
}
public void handle(Callback[] callbacks) throws IOException,
UnsupportedCallbackException {
WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
pc.setPassword(password);
}
}
好,到现在为可以启动服务并调用服务了,这只是一个简单的代码实现,希望有兴趣的朋友一起探讨。