這篇文章給大家分享的是shiro實(shí)現(xiàn)動(dòng)態(tài)刷新權(quán)限的詳細(xì)介紹,相信大部分人都還沒(méi)學(xué)會(huì)這個(gè)技能,為了讓大家學(xué)會(huì),給大家總結(jié)了以下內(nèi)容,話不多說(shuō),一起往下看吧。
創(chuàng)新互聯(lián)是一群有想法、有熱情,對(duì)互聯(lián)網(wǎng)抱有執(zhí)著信念的年輕人,愿用自己的智慧和熱情,幫助您使用好互聯(lián)網(wǎng)工具,成為您的建站英雄,成為您網(wǎng)站制作和網(wǎng)絡(luò)營(yíng)銷(xiāo)的“秘密武器”,專(zhuān)注于網(wǎng)站策劃、備案、空間域名、設(shè)計(jì)、后臺(tái)開(kāi)發(fā)、關(guān)鍵詞優(yōu)化排名、運(yùn)營(yíng)管理、維護(hù)服務(wù)、微信網(wǎng)站、移動(dòng)網(wǎng)站建設(shè),網(wǎng)站上線不是大家合作的終結(jié),相反,網(wǎng)站維護(hù)才剛剛開(kāi)始,我們期待常年累月的網(wǎng)站運(yùn)行過(guò)程總著為您提供更多的支持。我們致力于解決問(wèn)題,創(chuàng)造價(jià)值,不推諉,主動(dòng)承擔(dān)。
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean; import org.apache.shiro.web.filter.mgt.DefaultFilterChainManager; import org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver; import org.apache.shiro.web.servlet.AbstractShiroFilter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.qfx.demo.cache.MenuRoleCache; import com.qfx.demo.cache.MenuRoleCache2; import com.qfx.demo.vo.SysMenuRole; @Component public class ShiroPermissionSer { @Autowired ShiroFilterFactoryBean shiroFilterFactoryBean; private int count = 1; /** *
功能:動(dòng)態(tài)更新shiro權(quán)限(無(wú)需重啟)
* * @return */ public boolean updatePermission() { boolean flag = false; synchronized (shiroFilterFactoryBean) { AbstractShiroFilter shiroFilter = null; try { shiroFilter = (AbstractShiroFilter) shiroFilterFactoryBean.getObject(); PathMatchingFilterChainResolver filterChainResolver = (PathMatchingFilterChainResolver) shiroFilter.getFilterChainResolver(); DefaultFilterChainManager manager = (DefaultFilterChainManager) filterChainResolver.getFilterChainManager(); // 1. 清空老的權(quán)限控制 manager.getFilterChains().clear(); shiroFilterFactoryBean.getFilterChainDefinitionMap().clear(); // ========== 2. 動(dòng)態(tài)加載權(quán)限核心部分開(kāi)始 ========== // 后面這個(gè)可以直接從數(shù)據(jù)庫(kù)里面獲取 MapfilterChainDefinitionMap = new LinkedHashMap (); // 對(duì)靜態(tài)資源設(shè)置匿名訪問(wèn),從resoutces/static后面開(kāi)始寫(xiě) filterChainDefinitionMap.put("/css/**", "anon"); // 可匿名訪問(wèn)的地址 filterChainDefinitionMap.put("/", "anon"); filterChainDefinitionMap.put("/index.jsp", "anon"); filterChainDefinitionMap.put("/login/loginPage", "anon"); filterChainDefinitionMap.put("/login/register", "anon"); filterChainDefinitionMap.put("/login/login", "anon"); // 請(qǐng)求 logout.do地址,shiro去清除session filterChainDefinitionMap.put("/logout", "logout"); //循環(huán)url,逐個(gè)添加到section中。section就是filterChainDefinitionMap, //里面的鍵就是鏈接URL,值就是存在什么條件才能訪問(wèn)該鏈接(正式環(huán)境從數(shù)據(jù)庫(kù)獲取,這里模擬數(shù)據(jù)權(quán)限切換) if (count == 1) { Map menuRoleMap = MenuRoleCache2.menuRoleCacheMap; for (String key : menuRoleMap.keySet()) { filterChainDefinitionMap.put(key, "roles["+menuRoleMap.get(key).getRoleNames()+"]"); } count = 0; } else { Map menuRoleMap = MenuRoleCache.menuRoleCacheMap; for (String key : menuRoleMap.keySet()) { filterChainDefinitionMap.put(key, "roles["+menuRoleMap.get(key).getRoleNames()+"]"); } count = 1; } //所有url都必須認(rèn)證通過(guò)才可以訪問(wèn),必須放在最后 filterChainDefinitionMap.put("/**", "authc"); shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); // ========== 2. 動(dòng)態(tài)加載權(quán)限核心部分結(jié)束 ========== // 3. 重新構(gòu)建生成 Map chains = shiroFilterFactoryBean.getFilterChainDefinitionMap(); for (Map.Entry entry : chains.entrySet()) { String url = entry.getKey(); String chainDefinition = entry.getValue().trim().replace(" ", ""); manager.createChain(url, chainDefinition); } flag = true; System.out.println("更新權(quán)限成功"); } catch (Exception e) { throw new RuntimeException("更新shiro權(quán)限出現(xiàn)錯(cuò)誤!"); } } return flag; } }
這篇文章主要為大家詳細(xì)介紹了shiro實(shí)現(xiàn)動(dòng)態(tài)刷新權(quán)限,文中示例代碼介紹的非常詳細(xì),零基礎(chǔ)也能參考此文章,感興趣的小伙伴們可以參考一下。