日期:2014-05-19 浏览次数:20758 次
package axis.ws.testing; 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 ClientPasswordCallback implements CallbackHandler { private String userid; private String password; public String getUserid() { return userid; } public void setUserid(String userid) { this.userid = userid; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { WSPasswordCallback pc = (WSPasswordCallback) callbacks[0]; // ApplicationContext context= SpringUtil.getContext(); // PluginConfig cfg = (PluginConfig) context.getBean("globalCfg"); pc.setIdentifier(this.getUserid()); pc.setPassword(this.getPassword()); } }
package service; import org.apache.axis2.context.ServiceContext; import org.apache.axis2.context.MessageContext; public class LoginService { public boolean login(String username, String password) { if("bill".equals(username) && "1234".equals(password)) { // 第1步:设置key-value 对 MessageContext mc = MessageContext.getCurrentMessageContext(); ServiceContext sc = mc.getServiceContext(); sc.setProperty("login", "成功登录"); return true; } else { return false; } } public String getLoginMsg() { // 第1步:获得key-value 对中的value MessageContext mc = MessageContext.getCurrentMessageContext(); ServiceContext sc = mc.getServiceContext(); return (String)sc.getProperty("login"); } }
------解决方案--------------------
使用如下的命令生成客户端使用的stub 类:
%AXIS2_HOME%\bin\wsdl2java -uri http://localhost:8080/axis2/services/loginService?wsdl -p client -s -o stub
在stub\src\client 目录中生成了一个LoginServiceStub.java 类,在该类中找到如下的构造句法:
public LoginServiceStub(org.apache.axis2.context.ConfigurationContext configurationContext, String targetEndpoint, boolean useSeparateListener) throws org.apache.axis2.AxisFault
{
_serviceClient.getOptions().setSoapVersionURI( org.apache.axiom.soap.SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);
}
在该方法中最后添加如下的代码:
// 第3 步:打开客户端的Session 管理功能
_serviceClient.getOptions().setManageSession(true);
下面的客户端代码使用LoginServiceStub 对象访问了刚才建立的WebService:
LoginServiceStub stub = new LoginServiceStub();
LoginServiceStub.Login login = new Logi