加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

JAX-WS实做webservice验证

发布时间:2020-12-16 22:20:58 所属栏目:安全 来源:网络整理
导读:在JAX WS标准中,做websevice变得容易,都是用注解等就可以实现了,其中用来做? webservice的权限也是很容易的,比如要根据用户名和密码才能访问ws,下面直接代码,? 给出对应的例子,使用的是cxf了.? 1 ws接口类? ?? Java代码?? import ?javax.jws.WebMethod;?? im
在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. }???

2 WS 接口实现类:?
Java代码??
    @WebService(endpointInterface?=?"com.ws.HelloWorld")??
  1. class?HelloWorldImpl?implements?HelloWorld?{??
  2. @Resource??
  3. ????WebServiceContext?wsctx;??
  4. @Override??
  5. ????public?String?getHelloWorldMessage()?{??
  6. ????????MessageContext?mctx?=?wsctx.getMessageContext();??
  7. ????????//?取得报文头??
  8. ????????Map?http_headers?=??
  9. ????????????(Map)?mctx.get(??
  10. ????????????MessageContext.HTTP_REQUEST_HEADERS);??
  11. ????????List<String>?userList?=?(List)?http_headers.get("Username");??
  12. ????????List<String>?passList?=?(List)?http_headers.get("Password");??
  13. ????????String?username?=?"";??
  14. ????????String?password?=?"";??
  15. ????????if?(userList?!=?null)?{??
  16. ????????????username?=?userList.get(0);??
  17. ????????}??
  18. if?(passList?!=? ????????????password?=?passList.get(if?(username.equals("test")&&password.equals("password"))?{??
  19. ????????????return?"Hello?"??
  20. ????????????????+?username?+??
  21. ????????????????"?to?world?of?Jax?WS?-?Valid?User!";??
  22. ????????}?else?{??
  23. return?"?User?No?Valid!";??
  24. ????}??
  25. ? 其中,其实就是取出header的信息取进行判断是否有这个权限了,很容易明白.?
    3 然后是发布这个ws?
    static?void?main(String[]?args){??
  26. ?????Endpoint.publish(??
  27. ?????"http://localhost:9000/ws/hello",?new?HelloWorldImpl());??
  28. ?????
  29. ?}??

4 客户端去调用这个WS,其中注意要用户名和密码的传递,这里为了简单,没用证书等了,sans-serif; font-size:14px; line-height:25.2px">只是示意,实际上是复杂多了:?
?
?class?HelloWorldClient?{??
  • private?final?String?WS_URL?=??
  • ????????????"http://localhost:9000/ws/hello?wsdl";??
  • void?main(String[]?args)?throws?Exception?{??
  • ????????URL?url?=?new?URL(WS_URL);??
  • ????????QName?qname?=?new?QName(??
  • ????????????"http://ws.test.com/",??
  • ????????????"HelloWorldImplService");??
  • ????????Service?service?=?Service.create(url,?qname);??
  • ????????HelloWorld?hello?=?service.getPort(HelloWorld.class);??
  • ????????BindingProvider?provider?=?(BindingProvider)?hello;??
  • ????????Map<String,?Object>?req_ctx?=?provider.getRequestContext();??
  • ????????req_ctx.put(??
  • ????????BindingProvider.ENDPOINT_ADDRESS_PROPERTY,?WS_URL);??
  • //调用的用户名和密码,用map??
  • Map<String,?List<String>>?headers?=?new?HashMap<String,?List<String>>();??
  • ????????headers.put("Username",?Collections.singletonList("test"));??
  • ????????headers.put("Password",250)"> ????????????Collections.singletonList("password"));??
  • ????????req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS,?headers);??
  • }??

  • ? 其实核心就是:?
    ? headers.put("Username",Collections.singletonList("test"));?
    ??????? headers.put("Password",sans-serif; font-size:14px; line-height:25.2px">??????????? Collections.singletonList("password"));?
    ??????? req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS,headers);?

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


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

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读