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

构建基于CXF的WebService服务(3)-- 利用拦截器实现权限验证

发布时间:2020-12-16 23:07:56 所属栏目:安全 来源:网络整理
导读:CXF中的拦截器分为in拦截器和out拦截器,又有客户端拦截器和服务端拦截器。 拦截器使用流程:客户端(out)- 服务端(in)-处理业务-服务端(out)-客户端(in),并不是每一步都需要拦截器。在这里我们用到的是客户端Out拦截器和服务端in拦截器。服务端in拦

CXF中的拦截器分为in拦截器和out拦截器,又有客户端拦截器和服务端拦截器。

拦截器使用流程:客户端(out)-> 服务端(in)->处理业务->服务端(out)->客户端(in),并不是每一步都需要拦截器。在这里我们用到的是客户端Out拦截器和服务端in拦截器。服务端in拦截器检查用户级权限,客户端out浏览器发送用户信息给服务端。

1、创建服务端验证

JaxWsServerFactoryBean或Endpoint都可以通过getInInterceptors方法,向WebService服务添加拦截器。

1.1 Endpoint方式

[java]? view plain copy

在CODE上查看代码片

派生到我的代码片

  1. HelloWorldService?service?=?new?HelloWorldServiceImpl();??
  2. String?address?=?"http://localhost:8080/hello";??
  3. ??
  4. EndpointImpl?endpoint?=?(EndpointImpl)Endpoint.publish(address,?service);??
  5. endpoint.getInInterceptors().add(new?AuthInterceptor());??
1.2? JaxWsServerFactoryBean 方式

派生到我的代码片

    HelloWorldServiceImpl?impl?=? JaxWsServerFactoryBean?factory?=?new?JaxWsServerFactoryBean();??
  1. factory.setAddress("http://localhost:8080/hello");??
  2. factory.setServiceClass(HelloWorldService.class);??
  3. factory.setServiceBean(impl);??
  4. factory.getInInterceptors().add(new?AuthInterceptor());??
  5. factory.create();??


1.3 利用自定义拦截器实现权限控制
自定义拦截去需要实现PhaseInterceptor接口,不过一般都是继承自AbstractPhaseInterceptor<T>,下面我们来实现AuthInterceptor的权限控制功能

派生到我的代码片

    package?com.tiamaes.webservice.auth;??
  1. ??
  2. import?java.util.List;??
  3. import?org.apache.cxf.binding.soap.SoapMessage;??
  4. import?org.apache.cxf.headers.Header;??
  5. import?org.apache.cxf.interceptor.Fault;??
  6. import?org.apache.cxf.phase.AbstractPhaseInterceptor;??
  7. import?org.apache.cxf.phase.Phase;??
  8. import?org.w3c.dom.Element;??
  9. import?org.w3c.dom.NodeList;??
  10. /**???
  11. ?*?<p>类描述:用户权限验证拦截器??</p>?
  12. ?*?<p>修改人:王成委?</p>?
  13. ?*?<p>修改时间:2014-5-10?下午03:16:16??</p>?
  14. ?*?@version???
  15. ?*/??
  16. public?class?AuthInterceptor?extends?AbstractPhaseInterceptor<SoapMessage>?{??
  17. ????//在调用之前拦截??
  18. ????public?AuthInterceptor()?{??
  19. ????????super(Phase.PRE_INVOKE);??
  20. ????}??
  21. ????/**?
  22. ?????*?自定义拦截器需要实现handleMessage方法,该方法抛出Fault异常,可以自定义异常集成自Fault,?
  23. ?????*?也可以new?Fault(new?Throwable())?
  24. ?????*/??
  25. void?handleMessage(SoapMessage?soap)?throws?Fault?{??
  26. ????????System.out.println("开始验证用户信息");??
  27. ????????List<Header>?headers?=?soap.getHeaders();??
  28. ??????????
  29. ????????//检查headers是否存在??
  30. if(headers?==?null?|?headers.size()<1){??
  31. ????????????throw?new?Fault(new?IllegalArgumentException("找不到Header,无法验证用户信息"));??
  32. ????????}??
  33. ??????????
  34. ????????Header?header?=?headers.get(0);??
  35. ????????Element?el?=?(Element)header.getObject();??
  36. ????????NodeList?users?=?el.getElementsByTagName("username");??
  37. ????????NodeList?passwords?=?el.getElementsByTagName("password");??
  38. ????????//检查是否有用户名和密码元素??
  39. ????????if(users.getLength()<1){??
  40. ????????????new?IllegalArgumentException("找不到用户信息"));??
  41. ????????}??
  42. ????????String?username?=?users.item(0).getTextContent().trim();??
  43. if(passwords.getLength()<new?IllegalArgumentException("找不到密码信息"));??
  44. ????????String?password?=?passwords.item(0).getTextContent();??
  45. //检查用户名和密码是否正确??
  46. if(!"admin".equals(username)?||?!"admin".equals(password)){??
  47. new?IllegalArgumentException("用户名或密码不正确"));??
  48. ????????}else{??
  49. ????????????System.out.println("用户名密码正确允许访问");??
  50. }??

2、客户端发送用户信息

客户端则需要添加out拦截器,在out拦截器中加入消息头


客户端拦截器:ClienLoginInterceptor

派生到我的代码片

    import?javax.xml.namespace.QName;??
  1. import?org.apache.cxf.helpers.DOMUtils;??
  2. import?org.apache.cxf.interceptor.Fault;??
  3. import?org.apache.cxf.phase.AbstractPhaseInterceptor;??
  4. import?org.apache.cxf.phase.Phase;??
  5. import?org.w3c.dom.Document;??
  6. /**???
  7. ?*?<p>类描述:??</p>?
  8. ?*?<p>修改记录?----------------?</p>?
  9. ?*?<p>修改时间:2014-5-10?下午03:58:10??</p>?
  10. ?*?<p>修改备注:??</p>?
  11. ?*?@version???
  12. ?*/??
  13. class?ClientLoginInterceptor?extends?AbstractPhaseInterceptor<SoapMessage>?{??
  14. private?String?username;??
  15. ????private?String?password;??
  16. void?setUsername(String?username)?{??
  17. this.username?=?username;??
  18. void?setPassword(String?password)?{??
  19. this.password?=?password;??
  20. ????}??
  21. ?????*?创建一个新的实例?ClientLoginInterceptor.?
  22. ?????*?
  23. ?????*?@param?username?
  24. ?????*?@param?password?
  25. public?ClientLoginInterceptor(String?username,?String?password)?{??
  26. super(Phase.PREPARE_SEND);??
  27. this.username?=?username;??
  28. this.password?=?password;??
  29. /*?(non-Javadoc)?
  30. ?????*?@see?org.apache.cxf.interceptor.Interceptor#handleMessage(org.apache.cxf.message.Message)?
  31. //?TODO?Auto-generated?method?stub??
  32. ????????Document?doc?=?DOMUtils.createDocument();??
  33. ????????Element?auth?=?doc.createElement("authrity");??
  34. ????????Element?username?=?doc.createElement("username");??
  35. ????????Element?password?=?doc.createElement("password");??
  36. ????????username.setTextContent(this.username);??
  37. ????????password.setTextContent(this.password);??
  38. ????????auth.appendChild(username);??
  39. ????????auth.appendChild(password);??
  40. //doc.appendChild(auth);??
  41. ????????headers.add(0,?new?Header(new?QName("tiamaes"),auth));??
  42. }??

客户端添加ClientLoginInterceptor

派生到我的代码片

    static?void?main(String[]?args)?{??
  1. ????JaxWsDynamicClientFactory?dcf?=?JaxWsDynamicClientFactory.newInstance();??
  2. ????Client?client?=?dcf.createClient("http://127.0.0.1:8080/hello?wsdl");??
  3. ????client.getOutInterceptors().add(new?ClientLoginInterceptor("admin",?"admin"));??
  4. try?{??
  5. ????????Object[]?objs?=?client.invoke("syaHello",?"Tom");??
  6. ????????System.out.println(objs[0].toString());??
  7. ????}?catch?(Exception?e)?{??
  8. ????????e.printStackTrace();??
  9. ????}??

    } ?
转自:http://blog.csdn.net/jaune161/article/details/25602655

(编辑:李大同)

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

    推荐文章
      热点阅读