axis2 客户端 如何加入用户名密码验证 ?
Axis2生成了客户端代码,但是服务端要求用户名、密码验证,不知道客户端如何加入。
客户端的CallbackHandler类如下,但是不知道如何加入soap的head中,在CXF中可以容易的处理,但是Axis2就不知道怎么处理了。请各位施以援手。
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 LoginServiceStub.Login();
login.setUsername("bill");
login.setPassword("1234");
if(stub.login(login).local_return)
{
System.out.println(stub.getLoginMsg().local_return);
}
运行上面的代码后,会输出“成功登录”信息
[解决办法]
看到你的评论了,问题应该已经解决了吧。
在axis2中加入soap的登录信息到header,代码示例如下:
String username = "admin";//服务器端分配的用户名 String password = "1";//服务器端分配的密码 ServiceClient client = stub._getServiceClient(); SOAP11Factory factory = new SOAP11Factory(); OMNamespace SecurityElementNamespace = factory .createOMNamespace( "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "wsse"); OMElement usernameTokenEl = factory.createOMElement("UsernameToken", SecurityElementNamespace); OMElement usernameEl = factory.createOMElement("Username", SecurityElementNamespace); OMElement passwordEl = factory.createOMElement("Password", SecurityElementNamespace); OMElement actionEl = factory.createOMElement("Action", SecurityElementNamespace); passwordEl .addAttribute(factory .createOMAttribute( "Type", null, "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText")); usernameEl.setText(username); passwordEl.setText(password); usernameTokenEl.addChild(usernameEl); usernameTokenEl.addChild(passwordEl); usernameTokenEl.addChild(actionEl); SOAPHeaderBlockImpl block = new SOAP11HeaderBlockImpl("Security", SecurityElementNamespace, factory); block.addChild(usernameTokenEl); client.addHeader(block);