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

J2EE+Flex的菜单及权限控制实践

发布时间:2020-12-15 05:06:31 所属栏目:百科 来源:网络整理
导读:本文讲述了J2EE+Flex的一些开发心得。作者一直是搞J2EE的,使用了blazeds,Flex通过RemoteObject调用Java的后台方法。这样的一个最大的好处就是不再需要struts这样之类的框架了,可以直接使用spring中的bean。 AD: 最近学习了下Flex,我一直是搞J2EE的。所

本文讲述了J2EE+Flex的一些开发心得。作者一直是搞J2EE的,使用了blazeds,Flex通过RemoteObject调用Java的后台方法。这样的一个最大的好处就是不再需要struts这样之类的框架了,可以直接使用spring中的bean。

AD:

最近学习了下Flex,我一直是搞J2EE的。所以想整合试着开发,J2EE+Flex在网上查了些资料,有好几种方法。我这里使用的是blazeds,Flex通过RemoteObject调用Java的后台方法。我个人觉得这样的一个最大的好处就是不再需要struts这样之类的框架了,可以直接使用spring中的bean。要使用spring就必须先说下spring的整合问题,其实这个网上也有,只要就是一个SpringFactory类,这个类要实现FlexFactory接口,然后在WEB-INF/flex/services-config.xml中注册改factory。代码如下:

   
   
  1. <?factories>?
  2. ?<?factory?id="springContext"?class="com.wangmeng.flex.SpringFactory"><?/FACTORY>?
  3. <?/FACTORIES>?

这样配置好以后在WEB-INF/flex/remote-config.xml中只要把factory的名字写成和上面配置对应的名字如:springContext,source的值配置为spring中bean的id就可以了。例如:

<?destination?id="userLoginService">?
    
    
  • ??<?properties>?
  • ?????<?factory>springContext<?/FACTORY>?
  • ?????userLoginService<?/SOURCE>?
  • ??<?/PROPERTIES>?
  • ?< /DESTINATION>?
  • 具体SpringFactory类的源代码网上也有。

    下面我说下我构思的控制菜单及权限的方法:首先要控制肯定要有用户登录的环节,这里具体怎么实现都可以,当时登陆后要将用户的信息保存在session中,一遍在检查权限是使用。获取request,session都是通过flex.messaging.FlexContext提供的静态方法

    首先说菜单的控制,当用户打开首页,客户端远程调用加载菜单信息,当然这是没有登陆,只有一些公开的菜单可以看见,用户登录后可以再重新加载菜单,这是系统会根据用户的特权等级决定要返回的菜单列表(这里菜单的返回的数据来源你可以自己决定,可以放在数据库里也可以是其他的)。

    当然,只是这样控制肯定不够安全,那就是后面要说的对用spring中bean调用的控制:

    要控制spring中的bean不被越权调用当然要从前面的SpringFactory类着手啦,我们需要在每次调用bean之前通过bean的名字检查该用户是否有权调用,如果有权调用就返回该bean,如果没有权限就抛出一个没有权限的ServiceException类。还是具体看下我的实现代码吧,也许不是很优美,但是功能大致都实现了。

       
       
    1. package?com.wangmeng.flex; ?
    2. ?
    3. import?java.util.HashMap; ?
    4. import?java.util.List; ?
    5. ?
    6. import?javax.servlet.http.HttpSession; ?
    7. ?
    8. import?org.springframework.context.ApplicationContext; ?
    9. import?org.springframework.web.context.support.WebApplicationContextUtils; ?
    10. import?org.springframework.beans.BeansException; ?
    11. import?org.springframework.beans.factory.NoSuchBeanDefinitionException; ?
    12. ?
    13. import?com.wangmeng.web.data.SysPrivilege; ?
    14. import?com.wangmeng.web.data.User; ?
    15. import?com.wangmeng.web.service.privilege.PrivilegeService; ?
    16. ?
    17. import?flex.messaging.FactoryInstance; ?
    18. import?flex.messaging.FlexFactory; ?
    19. import?flex.messaging.config.ConfigMap; ?
    20. import?flex.messaging.services.ServiceException; ?
    21. ?
    22. public?class?SpringFactory?implements?FlexFactory?{ ?
    23. ?private?static?final?String?SOURCE?=?"source"; ?
    24. ?static?HashMap?beanMap?=?new?HashMap();//存放权限检查项 ?
    25. ?
    26. //在factory初始化是装在权限信息? ?
    27. ?
    28. void?initialize(String?id,?ConfigMap?configMap)?{ ?
    29. ??ApplicationContext?appContext?=?WebApplicationContextUtils ?
    30. ????.getWebApplicationContext(flex.messaging.FlexContext ?
    31. ??????.getServletConfig().getServletContext()); ?
    32. ??PrivilegeService?priviService?=?(PrivilegeService)?appContext ?
    33. ????.getBean("sysPrivilegeService"); ?
    34. ??List?priviList?=?priviService.listAll(); ?
    35. ??for?(Object?obj?:?priviList)?{ ?
    36. ???SysPrivilege?privi?=?(SysPrivilege)?obj; ?
    37. ???String?name?=?privi.getServiceName(); ?
    38. ???beanMap.put(name,?privi); ?
    39. ??} ?
    40. ?} ?
    41. ?
    42. ?public?FactoryInstance?createFactoryInstance(String?id,?ConfigMap?properties)?{ ?
    43. ??SpringFactoryInstance?instance?=?new?SpringFactoryInstance(this,?id,?
    44. ????properties); ?
    45. ??instance.setSource(properties.getPropertyAsString(SOURCE,?instance ?
    46. ????.getId())); ?
    47. ??return?instance; ?
    48. ?} ?
    49. ?
    50. ?public?Object?lookup(FactoryInstance?inst)?{ ?
    51. ??SpringFactoryInstance?factoryInstance?=?(SpringFactoryInstance)?inst; ?
    52. ??return?factoryInstance.lookup(); ?
    53. ?} ?
    54. ?
    55. ?class?SpringFactoryInstance?extends?FactoryInstance?{ ?
    56. ??SpringFactoryInstance(SpringFactory?factory,?String?id,?
    57. ????ConfigMap?properties)?{ ?
    58. ???super(factory,?properties); ?
    59. ??} ?
    60. ?
    61. ??public?String?toString()?{ ?
    62. ???return?"SpringFactory?instance?for?id="?+?getId()?+?"?source="?
    63. ?????+?getSource()?+?"?scope="?+?getScope(); ?
    64. ??} ?
    65. ?
    66. //在每次查找spring?bean之前检查权限。 ?
    67. ?
    68. ??public?Object?lookup()?{ ?
    69. ???String?beanName?=?getSource(); ?
    70. ???SysPrivilege?privi?=?(SysPrivilege)?beanMap.get(beanName); ?
    71. ???boolean?hasRight?=?false; ?
    72. ???if?(privi==null||privi.getLevel()?<=?0)?{ ?
    73. ????hasRight?=?true; ?
    74. ???}?else?{ ?
    75. ????HttpSession?session?=?flex.messaging.FlexContext ?
    76. ??????.getHttpRequest().getSession(); ?
    77. ????User?user?=?(User)?session.getAttribute("user"); ?
    78. ????if?(user?!=?null?&&?user.getPrivilege()?>=?privi.getLevel())?{ ?
    79. ?????hasRight?=?true; ?
    80. ????}?else?{ ?
    81. ?????hasRight?=?false; ?
    82. ????} ?
    83. ???} ?
    84. ???if?(hasRight)?{ ?
    85. ????ApplicationContext?appContext?=?WebApplicationContextUtils ?
    86. ??????.getWebApplicationContext(flex.messaging.FlexContext ?
    87. ????????.getServletConfig().getServletContext()); ?
    88. ????try?{ ?
    89. ?????return?appContext.getBean(beanName); ?
    90. ????}?catch?(NoSuchBeanDefinitionException?nexc)?{ ?
    91. ?????ServiceException?e?=?new?ServiceException(); ?
    92. ?????String?msg?=?"Spring?service?named?'"?+?beanName ?
    93. ???????+?"'?does?not?exist."; ?
    94. ?????e.setMessage(msg); ?
    95. ?????e.setRootCause(nexc); ?
    96. ?????e.setDetails(msg); ?
    97. ?????e.setCode("Server.Processing"); ?
    98. ?????throw?e; ?
    99. ????}?catch?(BeansException?bexc)?{ ?
    100. ?????ServiceException?e?=?new?ServiceException(); ?
    101. ?????String?msg?=?"Unable?to?create?Spring?service?named?'"?
    102. ???????+?beanName?+?"'?"; ?
    103. ?????e.setMessage(msg); ?
    104. ?????e.setRootCause(bexc); ?
    105. ?????e.setDetails(msg); ?
    106. ?????e.setCode("Server.Processing"); ?
    107. ?????throw?e; ?
    108. ????} ?
    109. ???}else{ ?
    110. ????ServiceException?e?=?new?ServiceException(); ?
    111. ????String?msg?=?"你没有足够的权限调用'"?
    112. ??????+?beanName?+?"'?"; ?
    113. ????e.setMessage(msg); ?
    114. ????e.setRootCause(null); ?
    115. ????e.setDetails(msg); ?
    116. ????e.setCode("Server.Processing"); ?
    117. ????throw?e; ?
    118. ???} ?
    119. ??} ?
    120. ?} ?
    121. ?
    122. } ?

    代码不是很难,稍微看下我想应该没有问题。这就是我的构思,如果真的要应用还有许多细节要考虑,至少一个大致的框架完成了。


    转载自:http://developer.51cto.com/art/200906/130840.htm

    (编辑:李大同)

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

      推荐文章
        热点阅读