首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

axis2 客户端 怎么加入用户名密码验证

2012-08-10 
axis2 客户端 如何加入用户名密码验证 ?Axis2生成了客户端代码,但是服务端要求用户名、密码验证,不知道客户

axis2 客户端 如何加入用户名密码验证 ?
Axis2生成了客户端代码,但是服务端要求用户名、密码验证,不知道客户端如何加入。
客户端的CallbackHandler类如下,但是不知道如何加入soap的head中,在CXF中可以容易的处理,但是Axis2就不知道怎么处理了。请各位施以援手。

Java code
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());    }}



[解决办法]
在客户端需要使用setManageSession(true)打开Session 管理功能。 

实现同一个WebService 的Session 管理需要如下三步: 

1. 使用MessageContext 和ServiceContext 获得与设置key-value 对。 

2. 为要进行Session 管理的WebService 类所对应的<service>元素添加一个scope 属性,并将该属性值设为 transportsession。 

3. 在客户端使用setManageSession(true)打开Session 管理功能。 

下面是一个在同一个WebService 类中管理Session 的例子。 

Java code
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,代码示例如下:

Java code
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); 

热点排行