JAX-WS实做webservice验证

来源:互联网 发布:织梦 添加视频不播放 编辑:程序博客网 时间:2024/04/29 11:25
在JAX WS标准中,做websevice变得容易,都是用注解等就可以实现了,其中用来做 
webservice的权限也是很容易的,比如要根据用户名和密码才能访问ws,下面直接代码, 
给出对应的例子,使用的是cxf了. 

1 ws接口类 
  
Java代码  收藏代码
  1. import javax.jws.WebMethod;  
  2. import javax.jws.WebService;  
  3. import javax.jws.soap.SOAPBinding;  
  4. import javax.jws.soap.SOAPBinding.Style;  
  5.   
  6.    
  7. @WebService  
  8. @SOAPBinding(style = Style.RPC)  
  9. public interface HelloWorld {  
  10.     @WebMethod  
  11.     String getHelloWorldMessage();  
  12.   
  13. }   


2 WS 接口实现类: 
  
Java代码  
  1. @WebService(endpointInterface = "com.ws.HelloWorld")  
  2. public class HelloWorldImpl implements HelloWorld {  
  3.     @Resource  
  4.     WebServiceContext wsctx;  
  5.     @Override  
  6.     public String getHelloWorldMessage() {  
  7.         MessageContext mctx = wsctx.getMessageContext();  
  8.   
  9.         // 取得报文头  
  10.   
  11.         Map http_headers =  
  12.   
  13.             (Map) mctx.get(  
  14.   
  15.             MessageContext.HTTP_REQUEST_HEADERS);  
  16.   
  17.         List<String> userList = (List) http_headers.get("Username");  
  18.   
  19.         List<String> passList = (List) http_headers.get("Password");  
  20.         String username = "";  
  21.         String password = "";  
  22.         if (userList != null) {  
  23.   
  24.             username = userList.get(0);  
  25.   
  26.         }  
  27.         if (passList != null) {  
  28.             password = passList.get(0);  
  29.   
  30.         }  
  31.   
  32.         if (username.equals("test")&&password.equals("password")) {  
  33.             return "Hello "  
  34.   
  35.                 + username +  
  36.   
  37.                 " to world of Jax WS - Valid User!";  
  38.   
  39.         } else {  
  40.   
  41.             return " User No Valid!";  
  42.   
  43.         }  
  44.   
  45.     }  
  46.   
  47.    
  48.   
  49. }   


  其中,其实就是取出header的信息取进行判断是否有这个权限了,很容易明白. 

3 然后是发布这个ws 
  
Java代码  收藏代码
  1. public static void main(String[] args){  
  2.   
  3.      Endpoint.publish(  
  4.   
  5.      "http://localhost:9000/ws/hello"new HelloWorldImpl());  
  6.   
  7.      
  8.  }  


4 客户端去调用这个WS,其中注意要用户名和密码的传递,这里为了简单,没用证书等了, 
只是示意,实际上是复杂多了: 
 
Java代码  
  1.  public class HelloWorldClient {  
  2.     private static final String WS_URL =  
  3.             "http://localhost:9000/ws/hello?wsdl";  
  4.   
  5.     public static void main(String[] args) throws Exception {  
  6.   
  7.         URL url = new URL(WS_URL);  
  8.   
  9.         QName qname = new QName(  
  10.   
  11.             "http://ws.test.com/",  
  12.             "HelloWorldImplService");  
  13.   
  14.         Service service = Service.create(url, qname);  
  15.   
  16.         HelloWorld hello = service.getPort(HelloWorld.class);  
  17.   
  18.   
  19.   
  20.         BindingProvider provider = (BindingProvider) hello;  
  21.   
  22.         Map<String, Object> req_ctx = provider.getRequestContext();  
  23.   
  24.         req_ctx.put(  
  25.   
  26.         BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WS_URL);  
  27.   
  28. //调用的用户名和密码,用map  
  29. Map<String, List<String>> headers = new HashMap<String, List<String>>();  
  30.   
  31.         headers.put("Username", Collections.singletonList("test"));  
  32.         headers.put("Password",  
  33.             Collections.singletonList("password"));  
  34.   
  35.         req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);  
  36.   
  37.   
  38.     }  
  39.   
  40. }  


  其实核心就是: 
  headers.put("Username", Collections.singletonList("test")); 
        headers.put("Password", 
            Collections.singletonList("password")); 
        req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers); 

在消息头中设置好MAP就可以了. 



另一个简单例子:http://blog.csdn.net/jackphang/article/details/8788178

0 0
原创粉丝点击