IBMSOFT

          ibmsoft 努力不一定成功,放棄一定失敗! 堅持自己的理想,實現自己的目標! 有好的想法就要出想辦法實現!

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            8 Posts :: 2 Stories :: 3 Comments :: 0 Trackbacks
          http://forum.springside.org.cn/viewthread.php?tid=927&highlight=%E6%9D%83%E9%99%90
          簡單實用一分鐘上手級權限控制

          找回來自己以前的一個項目, 用的是通過filter過濾來管理權限的方法, 很簡單,但也很實用。 這個項目并不小,但這么一個類就已經可以滿足其權限管理的需要了,所以其實很多時候,權限管理大家并不必要想得那么復雜, 對于不少系統,簡單通過filter來管理就ok了, simple 也是一種美^_^ 在web.xml里加入

          1. <!--================權限?設置================-->??
          2. <filter>??
          3. ????<filter-name>Authentication</filter-name>??
          4. ????<filter-class>com.springside.demo.security.UrlFilter</filter-class>??
          5. ????<init-param>??
          6. ????????<param-name>onError</param-name>??
          7. ????????<param-value>/login.jsp</param-value>??
          8. ????</init-param>??
          9. </filter>??
          10. <filter-mapping>??
          11. ????<filter-name>Authentication</filter-name>??
          12. ????<!--?只過濾?.jsp?結尾的url,?其余的如?.do,?.html,?.jpg,?.css?等不作過濾-->??
          13. ????<url-pattern>*.jsp</url-pattern>??
          14. </filter-mapping>??

          ?

          UrlFilter filter類的實現

          1. public?class?UrlFilter?implements?Filter?{??
          2. ????private?FilterConfig?filterConfig;??
          3. ??
          4. ????private?FilterChain?chain;??
          5. ??
          6. ????private?HttpServletRequest?request;??
          7. ??
          8. ????private?HttpServletResponse?response;??
          9. ??
          10. ????public?void?destroy()?{??
          11. ????????this.filterConfig?=?null;??
          12. ????}??
          13. ??
          14. ????public?void?init(FilterConfig?filterConfig)?throws?ServletException?{??
          15. ????????this.filterConfig?=?filterConfig;??
          16. ????}??
          17. ??
          18. ????public?void?doFilter(ServletRequest?servletRequest,??
          19. ????????????ServletResponse?servletResponse,?FilterChain?chain)??
          20. ????????????throws?IOException,?ServletException?{??
          21. ????????this.chain?=?chain;??
          22. ????????this.request?=?(HttpServletRequest)?servletRequest;??
          23. ????????this.response?=?((HttpServletResponse)?servletResponse);??
          24. ??
          25. ????????String?url?=?request.getServletPath();??
          26. ????????if?(url?==?null)??
          27. ????????????url?=?"";??
          28. ??
          29. ????????//?獲取session中的loginuser對象??
          30. ????????HttpSession?session?=?request.getSession();??
          31. ????????LoginUser?loginuser?=?(LoginUser)?session.getAttribute("loginuser");??
          32. ??
          33. ????????if?(baseUrl(url,?request))?{??
          34. ????????????//?如果是登陸界面等無須<u><b><font?color="#FF0000">權限</font></b></u>訪問的的公用界面則跳過??
          35. ????????????chain.doFilter(request,?response);??
          36. ????????}?else?if?(loginuser?==?null)?{??
          37. ????????????checkLogin(url);??
          38. ????????}?else?{??
          39. ????????????verifyUrl(url,?loginuser);??
          40. ????????}??
          41. ????}??
          42. ??
          43. ????private?void?checkLogin(String?url)?throws?ServletException,?IOException?{??
          44. ????????//?如果session中獲取不到?loginuser?對象,要不就是session?過期了,要不就是還沒登陸。所以返回登陸界面??
          45. ????????//?在登陸后記得把?loginuser?對象置于?session中??
          46. ??
          47. ????????if?(url.indexOf("/index.jsp")?>=?0??
          48. ????????????????&&?"login".equals(request.getParameter("act")))?{??
          49. ????????????//?獲取request中username,password??
          50. ????????????String?username?=?request.getParameter("username");??
          51. ????????????String?password?=?request.getParameter("password");??
          52. ????????????UserDao?userDao?=?new?UserDao();??
          53. ????????????if?(userDao.authUser(username,?password))?{??
          54. ????????????????LoginUser?user?=?userDao.getUser(username);??
          55. ????????????????request.getSession().setAttribute("loginuser",?user);??
          56. ????????????????verifyUrl(url,user);??
          57. ????????????????return;??
          58. ????????????}??
          59. ????????}??
          60. ????????response.sendRedirect("login.jsp");??
          61. ????}??
          62. ??
          63. ????private?void?verifyUrl(String?url,?LoginUser?loginuser)??
          64. ????????????throws?IOException,?ServletException?{??
          65. ????????//?獲取?loginuser?擁有的所有資源串??
          66. ????????Set?royurl?=?loginuser.getResStrings();??
          67. ????????if?(royurl?!=?null?&&?royurl.size()?>?0?&&?pass(royurl,?url,?request.getParameterMap()))?{??
          68. ????????????chain.doFilter(request,?response);??
          69. ????????}?else?{??
          70. ????????????response.setContentType("text/html;charset=GBK");??
          71. ????????????response??
          72. ????????????????????.getWriter()??
          73. ????????????????????.println(??
          74. ????????????????????????????"<div?style='margin:?100?auto;text-align:?center;"??
          75. ????????????????????????????????????+?"font:?bold?18px?宋體;color:?#0066CC;vertical-align:?middle'>?Sorry,您沒有<u><b><font?color="#FF0000">權限</font></b></u>訪問該資源!</div>");??
          76. ????????}??
          77. ????}??
          78. ??
          79. ????/**?
          80. ?????*?判斷是否是公用界面?
          81. ?????*/??
          82. ????protected?boolean?baseUrl(String?url,?HttpServletRequest?request)?{??
          83. ????????if?(url.indexOf("/login.jsp")?>=?0)?{??
          84. ????????????return?true;??
          85. ????????}??
          86. ????????return?false;??
          87. ????}??
          88. ??
          89. ????/**?
          90. ?????*?判斷該用戶是否有權請求該url?
          91. ?????*??
          92. ?????*?@param?royurl?
          93. ?????*????????????user擁有的授權的的url串集合?
          94. ?????*?@param?url?
          95. ?????*????????????當前請求的url?
          96. ?????*?@param?reqmap?
          97. ?????*????????????當前request的參數?
          98. ?????*?@return?是否通過該url?
          99. ?????*/??
          100. ????protected?boolean?pass(Set?royurl,?String?url,?Map?reqmap)?{??
          101. ????????boolean?match?=?true;??
          102. ????????for?(Iterator?iter?=?royurl.iterator();?iter.hasNext();)?{??
          103. ????????????//?獲取資源??
          104. ????????????match?=?true;??
          105. ????????????String?res_string?=?(String)?iter.next();??
          106. ????????????if?(res_string.indexOf("*")?>?0)?{??
          107. ????????????????res_string?=?res_string.substring(0,?res_string.indexOf("*"));??
          108. ????????????????if?(url.substring(0,?res_string.length()).equalsIgnoreCase(??
          109. ????????????????????????res_string))?{??
          110. ????????????????????return?true;?//?增加通配符比較??
          111. ????????????????}??
          112. ????????????}??
          113. ????????????//?分割url與參數??
          114. ????????????String[]?spw?=?res_string.split("\\?");?//?用"\\?"?轉義后即可得到正確的結??
          115. ????????????if?(!url.equalsIgnoreCase(spw[0]))?{??
          116. ????????????????match?=?false;??
          117. ????????????}??
          118. ????????????if?(match?&&?spw.length?>?1)?{??
          119. ????????????????String[]?spa?=?spw[1].split("\\&");?//?分拆各參數??
          120. ????????????????for?(int?j?=?0;?j?<?spa.length;?j++)?{??
          121. ????????????????????String[]?spe?=?spa[j].split("=");?//?分拆鍵與值??
          122. ????????????????????String?key?=?spe[0];??
          123. ????????????????????String?value?=?"";??
          124. ????????????????????if?(spe.length?>?1)?{??
          125. ????????????????????????value?=?spe[1].trim();??
          126. ????????????????????}??
          127. ??
          128. ????????????????????//?輪詢??
          129. ????????????????????String[]?values?=?(String[])?reqmap.get(key);??
          130. ????????????????????if?(values?!=?null)?{??
          131. ????????????????????????for?(int?k?=?0;?k?<?values.length;?k++)?{??
          132. ????????????????????????????if?(value.equalsIgnoreCase(values[k]))?{??
          133. ????????????????????????????????match?=?true;??
          134. ????????????????????????????????break;??
          135. ????????????????????????????}??
          136. ????????????????????????????match?=?false;??
          137. ????????????????????????}??
          138. ????????????????????????if?(!match)?{??
          139. ????????????????????????????break;??
          140. ????????????????????????}??
          141. ????????????????????}??
          142. ????????????????}??
          143. ??
          144. ????????????}??
          145. ??
          146. ????????????if?(match)?{??
          147. ????????????????break;??
          148. ????????????}??
          149. ????????}??
          150. ????????return?match;??
          151. ????}??
          152. ??
          153. ????public?static?void?main(String[]?args)?{??
          154. ????????UrlFilter?filter?=?new?UrlFilter();??
          155. ????????String?url?=?"/baseProd/product.do";??
          156. ??
          157. ????????Map?reqmap?=?new?HashMap();??
          158. ????????//?當前請求productline參數是11,12??
          159. ????????reqmap.put("productline",?new?String[]?{?"11",?"12"?});??
          160. ??
          161. ????????String?str;??
          162. ????????Set?royurl?=?new?HashSet();??
          163. ??
          164. ????????//?和授權的的url根本不同,false??
          165. ????????royurl.add("/user.do?a=1&b=2");??
          166. ????????System.out.println("match?false:"?+?filter.pass(royurl,?url,?reqmap));??
          167. ????????//?授權的請求參數13,14時?false??
          168. ????????royurl.add("/baseProd/product.do?productline=13&productline=14");??
          169. ????????System.out.println("match?false:"?+?filter.pass(royurl,?url,?reqmap));??
          170. ????????//?授權的請求參數11,13時?false??
          171. ????????royurl.add("/baseProd/product.do?productline=11&productline=13");??
          172. ????????System.out.println("match?false:"?+?filter.pass(royurl,?url,?reqmap));??
          173. ??
          174. ????????//?授權的請求參數11時?true??
          175. ????????royurl.add("/baseProd/product.do?productline=11");??
          176. ????????System.out.println("match?true:"?+?filter.pass(royurl,?url,?reqmap));??
          177. ??
          178. ????????//?參數的不論順序?true??
          179. ????????royurl.add("/baseProd/product.do?productline=12&productline=11");??
          180. ????????System.out.println("match?true:"?+?filter.pass(royurl,?url,?reqmap));??
          181. ??
          182. ????????royurl.clear();??
          183. ????????//?支持?"*"?號作通配符?true??
          184. ????????royurl.add("/baseProd/product.do*");??
          185. ????????System.out.println("match?ture:"?+?filter.pass(royurl,?url,?reqmap));??
          186. ??
          187. ????}??
          188. ??
          189. }??
          LoginUser 類:
          1. public?class?LoginUser?{??
          2. ????private?String?name;??
          3. ??????
          4. ????//用戶的授權url集合,如"/product.do?line=1&singer=2","/menu.do?son=1&son=2&son=3","/job.do*"??
          5. ????private?Set?resStrings;??
          6. ??
          7. ????public?String?getName()?{??
          8. ????????return?name;??
          9. ????}??
          10. ??
          11. ????public?void?setName(String?name)?{??
          12. ????????this.name?=?name;??
          13. ????}??
          14. ??
          15. ????public?Set?getResStrings()?{??
          16. ????????return?resStrings;??
          17. ????}??
          18. ??
          19. ????public?void?setResStrings(Set?resStrings)?{??
          20. ????????this.resStrings?=?resStrings;??
          21. ????}??
          22. ??????
          23. ??????
          24. }?

          posted on 2007-03-05 23:38 ibmsoft 閱讀(268) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 东山县| 筠连县| 宝清县| 罗源县| 遵化市| 大足县| 荃湾区| 尚志市| 阿拉善盟| 泗洪县| 微博| 额尔古纳市| 高安市| 象山县| 临洮县| 浦县| 高淳县| 顺平县| 石门县| 成安县| 广水市| 苏尼特右旗| 新疆| 台南市| 汝阳县| 保山市| 鄂伦春自治旗| 兴义市| 耿马| 土默特左旗| 临湘市| 隆回县| 泸水县| 商南县| 华坪县| 璧山县| 张家界市| 海伦市| 阿拉善盟| 神木县| 怀集县|