扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:中国IT实验室 来源:中国IT实验室 2007年9月22日
关键字:
在本页阅读全文(共2页)
4、 创建:
package example.ws_security;
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("ws_security", "keypassword");
}
public void handle(Callback[] callbacks) throws IOException,
UnsupportedCallbackException {
System.out.println("Handling Password!");
WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
String id = pc.getIdentifer();
System.out.println("id:"+id+" ,password:"+(String) passwords.get(id));
pc.setPassword((String) passwords.get(id));
}
}
5、 客户端实现:
package example.test;
import java.lang.reflect.Proxy;
import java.net.MalformedURLException;
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.handler.WSHandlerConstants;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.XFireProxy;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.security.wss4j.WSS4JOutHandler;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.codehaus.xfire.util.dom.DOMOutHandler;
import example.services.BookService;
import example.ws_security.PasswordHandler;
public class TTTest {
private WSS4JOutHandler wsOut;
private Service service;
private BookService bookservice;
private Client client;
private static final String SERVICE_URL ="http://localhost:8080/TT/services/BookService";
public TTTest()
{
//建议采用此种方式进行创建服务(带有服务名,此例为“BookService”)
service=new ObjectServiceFactory().create(BookService.class,
"BookService",
null,
null);
try {
bookservice=(BookService) new XFireProxyFactory().create(service, SERVICE_URL);
client = ((XFireProxy) Proxy.getInvocationHandler(bookservice)).getClient();
} catch (MalformedURLException e) {
e.printStackTrace();
}
wsOut = new WSS4JOutHandler();
wsOut.setProperty(WSHandlerConstants.SIG_PROP_FILE, "META-INF/xfire/outsecurity.properties");
wsOut.setProperty(WSHandlerConstants.ENC_PROP_FILE, "META-INF/xfire/outsecurity.properties");
wsOut.setProperty(WSHandlerConstants.USER, "ws_security");
//wsOut.setProperty("password", "keypassword11");
wsOut.setProperty(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PASSWORD_TEXT);
wsOut.setProperty(WSHandlerConstants.PW_CALLBACK_CLASS, PasswordHandler.class.getName());
wsOut.setProperty(WSHandlerConstants.SIG_KEY_ID,"IssuerSerial");
client.addOutHandler(new DOMOutHandler());
client.addOutHandler(wsOut);
String actions =WSHandlerConstants.SIGNATURE;
//wsOut.setProperty(WSHandlerConstants.TTL_TIMESTAMP,"30");
wsOut.setProperty(WSHandlerConstants.ACTION, actions);
System.out.println(bookservice.echo("Client test msg"));
client.close();
}
public static void main(String [] args)
{
new TTTest();
}
}
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者