动态调用webservice同时给webservice添加头部验证
答案:1 悬赏:40 手机版
解决时间 2021-01-10 10:10
- 提问者网友:爱唱彩虹
- 2021-01-09 10:39
动态调用webservice同时给webservice添加头部验证
最佳答案
- 五星知识达人网友:第四晚心情
- 2021-01-09 11:15
basic认证:我用的是XFIRE。1,首先修改services.xml
HelloWorld
com.ktsfot.ws.security.IHelloWorld
com.ktsfot.ws.security.HelloWorldImpl
application
2,AuthenticationHandler类的写法package com.ktsfot.ws.security;import org.codehaus.xfire.MessageContext;
import org.codehaus.xfire.handler.AbstractHandler;
import org.jdom.*;public class AuthenticationHandler extends AbstractHandler { public void invoke(MessageContext cfx) throws Exception {
// TODO Auto-generated method stub
if(cfx.getInMessage().getHeader() == null)
if(cfx.getInMessage().getHeader()==null){
throw new org.codehaus.xfire.fault.XFireFault("请求必须包含验证信息",org.codehaus.xfire.fault.XFireFault.SENDER);
}
Element token=cfx.getInMessage().getHeader().getChild("AuthenticationToken");
if (token == null){
throw new org.codehaus.xfire.fault.XFireFault("请求必须包含身份验证信息", org.codehaus.xfire.fault.XFireFault.SENDER);
} String username = token.getChild("Username").getValue();
String password = token.getChild("Password").getValue();
try{
//进行身份验证 ,只有abcd@1234的用户为授权用户
if(username.equals("abcd") && password.equals("1234"))
//这语句不显示
System.out.println("身份验证通过");
else throw new Exception();
}
catch (Exception e){
throw new org.codehaus.xfire.fault.XFireFault("非法的用户名和密码", org.codehaus.xfire.fault.XFireFault.SENDER);
}
}} 3,客户端调用:HelloWorldClient client = new HelloWorldClient();
//create a default service endpoint
HelloWorldPortType service = client.getHelloWorldHttpPort();
XFireProxy proxy = (XFireProxy)Proxy.getInvocationHandler(service);
proxy.getClient().addOutHandler(new ClientAuthenticationHandler("abcd", "1234"));
System.out.print(service.sayHello());
System.exit(0);
com.ktsfot.ws.security.HelloWorldImpl
import org.codehaus.xfire.handler.AbstractHandler;
import org.jdom.*;public class AuthenticationHandler extends AbstractHandler { public void invoke(MessageContext cfx) throws Exception {
// TODO Auto-generated method stub
if(cfx.getInMessage().getHeader() == null)
if(cfx.getInMessage().getHeader()==null){
throw new org.codehaus.xfire.fault.XFireFault("请求必须包含验证信息",org.codehaus.xfire.fault.XFireFault.SENDER);
}
Element token=cfx.getInMessage().getHeader().getChild("AuthenticationToken");
if (token == null){
throw new org.codehaus.xfire.fault.XFireFault("请求必须包含身份验证信息", org.codehaus.xfire.fault.XFireFault.SENDER);
} String username = token.getChild("Username").getValue();
String password = token.getChild("Password").getValue();
try{
//进行身份验证 ,只有abcd@1234的用户为授权用户
if(username.equals("abcd") && password.equals("1234"))
//这语句不显示
System.out.println("身份验证通过");
else throw new Exception();
}
catch (Exception e){
throw new org.codehaus.xfire.fault.XFireFault("非法的用户名和密码", org.codehaus.xfire.fault.XFireFault.SENDER);
}
}} 3,客户端调用:HelloWorldClient client = new HelloWorldClient();
//create a default service endpoint
HelloWorldPortType service = client.getHelloWorldHttpPort();
XFireProxy proxy = (XFireProxy)Proxy.getInvocationHandler(service);
proxy.getClient().addOutHandler(new ClientAuthenticationHandler("abcd", "1234"));
System.out.print(service.sayHello());
System.exit(0);
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯