`
kobe学java
  • 浏览: 249939 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

修改spring security源码实现动态授权

 
阅读更多

Java代码   收藏代码
  1. spring security 安全框架都是通过xml配置文件在容器启动时把资源和角色之间的许可信息加载到内存中,可往往我们需要通过在数据库中配置资源和角色的许可管理来实现动态授权,下面介绍一些通过修改springsecurity源代码的方法来实现动态授权。  
  2.   
  3.    
  4.   
  5. 首先下载spring security的源代码找到org.springframework.security.intercept.web.FilterSecurityInterceptor类,找到obtainObjectDefinitionSource方法,我们就是要通过修改该方法实现动态授权  
  6.   
  7.    
  8.   
  9.    
  10.   
  11.     private static FilterInvocationDefinitionSource s = null;  
  12.   
  13.     private ScRoleResDAO scRoleResDAO;  
  14.   
  15.     private ScResourceDAO scResourceDAO;  
  16.   
  17.    
  18.   
  19.     public ObjectDefinitionSource obtainObjectDefinitionSource() {  
  20.   
  21.    
  22.   
  23.     if (s == null){  
  24.   
  25.         UrlMatcher urlMatcher = new AntUrlPathMatcher(true);  
  26.   
  27.         LinkedHashMap requestMap = new LinkedHashMap();  
  28.   
  29.    
  30.   
  31. //          ApplicationContext context = new ClassPathXmlApplicationContext(  
  32.   
  33. //                 "spring/dataAccess.xml"  
  34.   
  35. //                 );  
  36.   
  37. //          ScRoleResDAO scRoleResDAO = (ScRoleResDAO)context.getBean("ScRoleResDAO");  
  38.   
  39. //           ScResourceDAO scResourceDAO = (ScResourceDAO)context.getBean("ScResourceDAO");  
  40.   
  41.    
  42.   
  43.         /* 一下这段代码可以忽视,因为每个人的实现方式都不同, 
  44.  
  45.          * 这段代码主要是从数据库中取得资源和角色信息的, 
  46.  
  47.          * 大家取出来的信息格式可能多种多样,关键是要把这些信息整理成统一的格式 
  48.  
  49.          * 参照下面第二段标注为关键代码的部分*/  
  50.   
  51.         Map<ScResource, List<ScRoleRes>> map = new TreeMap<ScResource, List<ScRoleRes>>();  
  52.   
  53.         ScResourceExample e = new ScResourceExample();  
  54.   
  55.         e.setOrderByClause("ORDER_NUM");  
  56.   
  57.         List<ScResource> resList = scResourceDAO.selectByExample(e);  
  58.   
  59.    
  60.   
  61.         ScRoleResExample example = null;  
  62.   
  63.         String url = "";  
  64.   
  65.         List<ScRoleRes> list = null;  
  66.   
  67.         for (ScResource resource : resList) {  
  68.   
  69.             url = resource.getUrl();  
  70.   
  71.             example = new ScRoleResExample();  
  72.   
  73.             example.createCriteria().andUrlEqualTo(url);  
  74.   
  75.             list = scRoleResDAO.selectByExample(example);  
  76.   
  77.             if (list != null){  
  78.   
  79.                map.put(resource, list);  
  80.   
  81.             }  
  82.   
  83.         }  
  84.   
  85.    
  86.   
  87.    
  88.   
  89.         /****************  以下是关键代码 *********************/  
  90.   
  91.    
  92.   
  93.         /* 
  94.  
  95.          * 这段代码的重点是生成 requestMap 
  96.  
  97.          * requestMap 的 key 的类型为 RequestKey ,需要包含资源的url 
  98.  
  99.          * requestMap 的 value 的类型为 ConfigAttributeDefinition ,它需要包含该资源可访问的角色列表信息 
  100.  
  101.          * 最后生成 DefaultFilterInvocationDefinitionSource 返回就可以了, 
  102.  
  103.          * 当然需要把这个结果保存在内存中,要不然画面上每次刷新都要从数据库中取得信息,资源消耗太大了 
  104.  
  105.          * 提供一个刷内存的方法,当数据库中的权限信息改变时要刷新内存中的旧的信息*/  
  106.   
  107.             Iterator iterator = map.entrySet().iterator();  
  108.   
  109.             ScResource key = null;  
  110.   
  111.             List<ScRoleRes> list1 = null;  
  112.   
  113.             List<ConfigAttribute> roles = null;  
  114.   
  115.             while (iterator.hasNext()) {  
  116.   
  117.                 Map.Entry entry = (Map.Entry) iterator.next();  
  118.   
  119.                 key = (ScResource)entry.getKey();  
  120.   
  121.                 RequestKey reqKey = new RequestKey(key.getUrl());  
  122.   
  123.                 list1 = (List<ScRoleRes>)entry.getValue();  
  124.   
  125.                 roles = new ArrayList<ConfigAttribute>();  
  126.   
  127.                 for (ScRoleRes res : list1) {  
  128.   
  129.                   roles.add(new SecurityConfig(res.getRole()));  
  130.   
  131.             }  
  132.   
  133.                 requestMap.put(reqKey, new ConfigAttributeDefinition(roles));  
  134.   
  135.             }  
  136.   
  137.             s = new DefaultFilterInvocationDefinitionSource(urlMatcher,requestMap);  
  138.   
  139.             /****************  以上是关键代码 *********************/  
  140.   
  141.     }  
  142.   
  143.         return s;  
  144.   
  145.     }  
  146.   
  147.    
  148.   
  149.     /** 
  150.  
  151.      * 提供一个刷新内存的静态方法 
  152.  
  153.      */  
  154.   
  155.     public static void refresh() {  
  156.   
  157.        s = null;  
  158.   
  159.    
  160.   
  161.    
  162.   
  163. 修改启动加载的xml文件,如果此文件是通过命名空间配置的,需要加入  
  164.   
  165.    
  166.   
  167.     <beans:bean id="_filterSecurityInterceptor"  
  168.   
  169.        class="org.springframework.security.intercept.web.FilterSecurityInterceptor"  
  170.   
  171.        p:authenticationManager-ref="_authenticationManager"  
  172.   
  173.         p:accessDecisionManager-ref="accessDecisionManager">  
  174.   
  175.         <beans:property name="objectDefinitionSource">  
  176.   
  177.            <beans:value><![CDATA[  
  178.   
  179.               CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON  
  180.   
  181.               PATTERN_TYPE_APACHE_ANT  
  182.   
  183.               /**=ROLE_SUPERVISOR  
  184.   
  185.            ]]></beans:value>  
  186.   
  187.         </beans:property>  
  188.   
  189.        <beans:property name="scRoleResDAO" ref="ScRoleResDAO"/>  
  190.   
  191.        <beans:property name="scResourceDAO" ref="ScResourceDAO"/>  
  192.   
  193.     </beans:bean>  
  194.   
  195.    
  196.   
  197. 其中objectDefinitionSource并没有实际意义,因为我们已经修改了源代码,如果不想注入这个,把FilterSecurityInterceptor类中的objectDefinitionSource变量去掉应该就可以了,不过没有试过  
  198.   
  199. 另外两个DAO是我根据我的代码注入的,大家换成自己的DAO就可以了。  

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics