欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
shiro安全框架擴展教程--如何動(dòng)態(tài)控制頁(yè)面節點(diǎn)元素的權限

         上些章節我們都學(xué)習了shiro中的各種實(shí)際應用技巧,今天我想講的是如何動(dòng)態(tài)控制頁(yè)面節點(diǎn)權限,相信這個(gè)控制對于很多玩權限的人來(lái)說(shuō)都是一個(gè)比較頭痛的,

因為這實(shí)在不怎么好統一控制的,現在我來(lái)展示下我通過(guò)shiro是如何實(shí)現的,算是拋磚引玉,希望大家有更好的解決方案,可以相互學(xué)習;下面就直接進(jìn)入主題不啰嗦了


之前我們已經(jīng)學(xué)習了如何在項目啟動(dòng)的時(shí)候加載所有的資源角色,包括第三方資源,下面我再帖上加長(cháng)加粗版的代碼方便說(shuō)明


  1. package com.silvery.security.shiro.service.impl;  
  2.   
  3. import java.text.MessageFormat;  
  4. import java.util.HashMap;  
  5. import java.util.HashSet;  
  6. import java.util.List;  
  7. import java.util.Map;  
  8. import java.util.Set;  
  9.   
  10. import org.apache.shiro.cache.Cache;  
  11. import org.springframework.beans.factory.annotation.Autowired;  
  12.   
  13. import com.silvery.project.cms.model.Authority;  
  14. import com.silvery.project.cms.model.Permission;  
  15. import com.silvery.project.cms.service.PermissionService;  
  16. import com.silvery.project.cms.vo.PermissionVo;  
  17. import com.silvery.security.shiro.cache.SimpleMapCache;  
  18. import com.silvery.security.shiro.cache.extend.SimpleCacheManager;  
  19. import com.silvery.security.variable.Const;  
  20.   
  21. /**  
  22.  *   
  23.  * 加載第三方角色資源配置服務(wù)類(lèi)  
  24.  *   
  25.  * @author shadow  
  26.  *   
  27.  */  
  28. public class SimpleFilterChainDefinitionsService extends AbstractFilterChainDefinitionsService {  
  29.   
  30.     @Autowired  
  31.     private SimpleCacheManager simpleCacheManager;  
  32.   
  33.     @Autowired  
  34.     private PermissionService permissionService;  
  35.   
  36.     @Override  
  37.     public Map<String, String> initOtherPermission() {  
  38.         return converResultMap(initOperation());  
  39.     }  
  40.   
  41.     @SuppressWarnings("unchecked")  
  42.     private Map<Object, Object> initOperation() {  
  43.   
  44.         Map<Object, Object> resultMap = new HashMap<Object, Object>();  
  45.   
  46.         // 加載數據庫所有資源  
  47.         PermissionVo vo = new PermissionVo();  
  48.         List<Permission> permissions = (List<Permission>) permissionService.query(vo).getValue();  
  49.   
  50.         List<Authority> authorities = null;  
  51.   
  52.         for (Permission permission : permissions) {  
  53.   
  54.             // 遍歷查詢(xún)當前資源的配置角色  
  55.             vo.setId(permission.getId());  
  56.             authorities = (List<Authority>) permissionService.query4authority(vo).getValue();  
  57.   
  58.             // 組裝角色集合  
  59.             Set<String> authoritySet = getPermissionSet(authorities);  
  60.   
  61.             if (authoritySet.isEmpty()) {  
  62.                 continue;  
  63.             }  
  64.             if (permission.getType() == 1) {  
  65.                 // 請求路徑資源處理  
  66.                 resultMap.put(permission.getContent(), MessageFormat.format(SHIRO_AUTHORITY_FORMAT, authoritySet));  
  67.             } else {  
  68.                 // 元素資源處理  
  69.                 Map<Object, Object> map = new HashMap<Object, Object>(1);  
  70.                 map.put(Const.OTHER_PERMISSSION_CACHE_NAME, authoritySet);  
  71.                 Cache<Object, Object> cache = new SimpleMapCache(Const.OTHER_PERMISSSION_CACHE_NAME, map);  
  72.                 simpleCacheManager.createCache(Const.OTHER_PERMISSSION_CACHE_NAME + "_" + permission.getId(), cache);  
  73.             }  
  74.         }  
  75.   
  76.         return resultMap;  
  77.     }  
  78.   
  79.     /** 獲取角色名稱(chēng)集合 */  
  80.     private Set<String> getPermissionSet(List<Authority> authorities) {  
  81.         Set<String> authoritieSet = new HashSet<String>(authorities.size());  
  82.         for (Authority authority : authorities) {  
  83.             authoritieSet.add(authority.getContent());  
  84.         }  
  85.         return authoritieSet;  
  86.     }  
  87.   
  88.     /** 泛型Object轉換String */  
  89.     private Map<String, String> converResultMap(Map<Object, Object> map) {  
  90.         Map<String, String> resultMap = new HashMap<String, String>(map.size());  
  91.         for (Map.Entry<Object, Object> entry : map.entrySet()) {  
  92.             resultMap.put(entry.getKey().toString(), entry.getValue().toString());  
  93.         }  
  94.         return resultMap;  
  95.     }  
  96.   
  97. }  

1. 加載所有資源,包括請求URL,元素節點(diǎn)等數據,我這里為了演示,沒(méi)有分那么細就只有兩種

2. 請求URL的直接放到框架中,形式如/user/list.do*=role[root,user],跟我們shiro.xml配置的一樣

3. 元素節點(diǎn)則相應放到以鍵值對的形式放到緩存,以節點(diǎn)的編號組合成key保證可以找到這個(gè)緩存對,值是相應的角色集合


我們的緩存就存在各個(gè)資源所需要的角色集合, 加載數據步驟已經(jīng)完畢了下面看看我們是怎么應用的


首先我是使用springMVC+freemarker作為展現層,具體怎么配置整合我也不說(shuō),百度一堆,直接看我帖代碼說(shuō)明


  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <title>歡迎主頁(yè)</title>  
  6. </head>  
  7. <body>  
  8.     ${username!"游客"}, 歡迎您的訪(fǎng)問(wèn)! <br />  
  9.     <hr />  
  10.     可選操作:  
  11.     <#if username??>  
  12.         <@sec id="2" body="<a href='/cms/user/page.do'>用戶(hù)列表</a> " /><a href="/cms/authority/page.do">權限列表</a> <a href="/cms/permission/page.do">資源列表</a> <a href="/cms/logout.do">注銷(xiāo)退出</a>   
  13.     <#else>  
  14.         <a href="#" onclick="top.location.href='/cms/logout.do';">前往登錄</a>   
  15.     </#if>  
  16.     <hr />  
  17. </body>  
  18. </html>  

很明顯看到我的頁(yè)面有一個(gè)@sec的標簽,然后有兩個(gè)參數,一個(gè)id,一個(gè)是body,至于id則是你資源的編號,body是需要元素節點(diǎn)內容


然后我們怎么通過(guò)這個(gè)標簽來(lái)判定是否在頁(yè)面渲染body的節點(diǎn)內容呢?


下面我們看看這個(gè)@sec的實(shí)現


  1. package com.silvery.core.freemarker;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.Map;  
  5. import java.util.Set;  
  6.   
  7. import org.apache.shiro.SecurityUtils;  
  8. import org.apache.shiro.cache.Cache;  
  9. import org.apache.shiro.subject.Subject;  
  10. import org.springframework.beans.factory.annotation.Autowired;  
  11.   
  12. import com.silvery.security.shiro.cache.extend.SimpleCacheManager;  
  13. import com.silvery.security.variable.Const;  
  14.   
  15. import freemarker.core.Environment;  
  16. import freemarker.template.TemplateDirectiveBody;  
  17. import freemarker.template.TemplateDirectiveModel;  
  18. import freemarker.template.TemplateException;  
  19. import freemarker.template.TemplateModel;  
  20.   
  21. /**  
  22.  *   
  23.  * FreeMarker自定義標簽,節點(diǎn)權限控制  
  24.  *   
  25.  * @author shadow  
  26.  *   
  27.  */  
  28. public class SecurityTag implements TemplateDirectiveModel {  
  29.   
  30.     @Autowired  
  31.     private SimpleCacheManager simpleCacheManager;  
  32.   
  33.     @SuppressWarnings("unchecked")  
  34.     public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody directiveBody)  
  35.             throws TemplateException, IOException {  
  36.   
  37.         Object id = params.get("id");  
  38.         Object body = params.get("body");  
  39.   
  40.         validate(id, body);  
  41.   
  42.         if (hasRole(id)) {  
  43.             env.getOut().write(body.toString());  
  44.         } else {  
  45.             env.getOut().write("");  
  46.         }  
  47.   
  48.     }  
  49.   
  50.     private void validate(Object id, Object body) throws TemplateException {  
  51.         if (id == null || id.toString().trim().equals("")) {  
  52.             throw new TemplateException("參數[id]不能為空", null);  
  53.         }  
  54.         if (body == null) {  
  55.             throw new TemplateException("參數[body]不能為空", null);  
  56.         }  
  57.     }  
  58.   
  59.     @SuppressWarnings("unchecked")  
  60.     private boolean hasRole(Object id) {  
  61.         Cache<Object, Object> cache = simpleCacheManager.getCache(Const.OTHER_PERMISSSION_CACHE_NAME + "_" + id);  
  62.         if (cache == null) {  
  63.             return false;  
  64.         } else {  
  65.             Object obj = cache.get(Const.OTHER_PERMISSSION_CACHE_NAME);  
  66.             if (obj == null) {  
  67.                 return false;  
  68.             }  
  69.             Set<String> authoritySet = (Set<String>) obj;  
  70.             Subject subject = SecurityUtils.getSubject();  
  71.             for (String authority : authoritySet) {  
  72.                 if (subject.hasRole(authority)) {  
  73.                     return true;  
  74.                 }  
  75.             }  
  76.         }  
  77.         return false;  
  78.     }  
  79.   
  80. }  

很清晰地看到,我們是用到之前的那個(gè)資源角色緩存,通過(guò)判定緩存中存在的角色與當前shiro認證用戶(hù)擁有的角色匹配,如存在任意相同角色則渲染相應節點(diǎn)


如Subject擁有角色[root,user],而x編號資源擁有[user,test]角色,很明顯有交集會(huì )認證通過(guò),反之則直接渲染空字符


大概流程就是這樣,有的人可能會(huì )問(wèn),如何配置這個(gè)tag實(shí)現類(lèi)呢?我帖下spring-mvc.xml的freemarker配置


  1. <!-- FreeMarker配置 -->  
  2.     <bean id="freemarkerConfig"  
  3.         class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  
  4.         <property name="templateLoaderPath" value="/WEB-INF/ftl/" />  
  5.         <property name="defaultEncoding" value="UTF-8" />  
  6.         <property name="freemarkerVariables">  
  7.             <map>  
  8.                 <entry key="sec">  
  9.                     <bean class="com.silvery.core.freemarker.SecurityTag">  
  10.                     </bean>  
  11.                 </entry>  
  12.             </map>  
  13.         </property>  
  14.         <property name="freemarkerSettings">  
  15.             <props>  
  16.                 <prop key="template_update_delay">10</prop>  
  17.                 <prop key="locale">zh_CN</prop>  
  18.                 <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>  
  19.                 <prop key="date_format">yyyy-MM-dd</prop>  
  20.                 <prop key="number_format">#.####</prop>  
  21.             </props>  
  22.         </property>  
  23.     </bean>  

相信懂freemarker的人都能看明白,我也不累贅說(shuō)明;最后再說(shuō)一句,謝謝大家支持.

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
JEECMS自定義標簽開(kāi)發(fā)步驟
Java 用Freemarker完美導出word文檔(帶圖片)
freemarker入門(mén)小例子
freemarker(1)老紫竹的第一個(gè)freemaker程序 - freemarker ...
freemarker入門(mén)例子
Spring發(fā)送郵件簡(jiǎn)單實(shí)例
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久