日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

springboot+shiro:ShiroConfiguration配置

發布時間:2025/3/12 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot+shiro:ShiroConfiguration配置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.EhCacheManager EhCache緩存管理也可將shiro session存入redis中

@Beanpublic EhCacheManager getEhCacheManager() {EhCacheManager em = new EhCacheManager();em.setCacheManagerConfigFile("classpath:ehcache-shiro.xml");return em;}

ehcache-shiro.xml中的配置

<?xml version="1.0" encoding="utf-8"?><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"><diskStore path="java.io.tmpdir"/><defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="30" timeToLiveSeconds="30" overflowToDisk="false"/><!--配置自定義緩存maxElementsInMemory:緩存中允許創建的最大對象數eternal:緩存中對象是否為永久的,如果是,超時設置將被忽略,對象從不過期。timeToIdleSeconds:緩存數據的鈍化時間,也就是在一個元素消亡之前,兩次訪問時間的最大時間間隔值,這只能在元素不是永久駐留時有效,如果該值是 0 就意味著元素可以停頓無窮長的時間。timeToLiveSeconds:緩存數據的生存時間,也就是一個元素從構建到消亡的最大時間間隔值,這只能在元素不是永久駐留時有效,如果該值是0就意味著元素可以停頓無窮長的時間。overflowToDisk:內存不足時,是否啟用磁盤緩存。memoryStoreEvictionPolicy:緩存滿了之后的淘汰算法。--><cache name="erpCache"maxElementsInMemory="10000"eternal="true"overflowToDisk="false"timeToIdleSeconds="0"timeToLiveSeconds="600"memoryStoreEvictionPolicy="LFU" /> </ehcache>

2.配置 LifecycleBeanPostProcessor(管理shiro Bean的生命周期)

public LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {return new LifecycleBeanPostProcessor();}

3.配置 DefaultAdvisorAutoProxyCreator(用來掃描上下文,尋找所有的Advistor,將這些Advistor應用到符合其定義的切入點的Bean中)

@Beanpublic DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {DefaultAdvisorAutoProxyCreator daap = new DefaultAdvisorAutoProxyCreator();daap.setProxyTargetClass(true);return daap;}

4.配置SecurityManager (管理器,管理subject及其相關的登陸驗證,授權等,需配置realm和緩存管理)

@Bean(name = "securityManager")public DefaultWebSecurityManager getDefaultWebSecurityManager(SystemAuthorizingRealm realm) {DefaultWebSecurityManager dwsm = new DefaultWebSecurityManager();dwsm.setRealm(realm); // <!-- 用戶授權/認證信息Cache, 采用EhCache 緩存 ,此處是使用EhCache,可換成redis緩存--> dwsm.setCacheManager(getEhCacheManager());return dwsm;}

5.配置 AuthorizationAttributeSourceAdvisor(開啟shiro spring aop 權限注解支持,即:@RequiresPermissions(“權限code”)

@Beanpublic AuthorizationAttributeSourceAdvisor getAuthorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager) {AuthorizationAttributeSourceAdvisor aasa = new AuthorizationAttributeSourceAdvisor();aasa.setSecurityManager(securityManager);return aasa;}

6.配置shiroFilter

@Bean(name = "shiroFilter")public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager securityManager, UserService userService,MaterialCategoryService materialCategoryMapper,PermissionsServcie permissionsServcie,OrgService orgService) {ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();// 必須設置 SecurityManager shiroFilterFactoryBean.setSecurityManager(securityManager);// 如果不設置默認會自動尋找Web工程根目錄下的"/login.jsp"頁面shiroFilterFactoryBean.setLoginUrl("/login");// 登錄成功后要跳轉的連接shiroFilterFactoryBean.setSuccessUrl("/user");shiroFilterFactoryBean.setUnauthorizedUrl("/403");//設置過濾鏈的私有方法loadShiroFilterChain(shiroFilterFactoryBean, userService,materialCategoryMapper,permissionsServcie,orgService);return shiroFilterFactoryBean;}

7.private loadShiroFilterChain 私有過濾鏈定義,供6使用

/*** 加載shiroFilter權限控制規則(從數據庫讀取然后配置)*/private void loadShiroFilterChain(ShiroFilterFactoryBean shiroFilterFactoryBean, UserService userService, MaterialCategoryService materialCategoryMapper, PermissionsServcie permissionsServcie, OrgService orgService) {//攔截規則,//CaptchaFormAuthenticationFilter extends FormAuthenticationFilter(shiro認證)//MapLogoutFilter extends org.apache.shiro.web.filter.authc.LogoutFilter(shiro Logout)Map<String, Filter> filters = shiroFilterFactoryBean.getFilters();filters.put("authc", new CaptchaFormAuthenticationFilter(userService,materialCategoryMapper, permissionsServcie,orgService));filters.put("logout", new MapLogoutFilter());/// 下面這些規則配置最好配置到配置文件中 ///Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();// authc:該過濾器下的頁面必須驗證后才能訪問,它是Shiro內置的一個攔截器org.apache.shiro.web.filter.authc.FormAuthenticationFilterfilterChainDefinitionMap.put("/myCode/**", "authc");// 這里為了測試,只限制/user,實際開發中請修改為具體攔截的請求規則// anon:它對應的過濾器里面是空的,什么都沒做logger.info("##################從數據庫讀取權限規則,加載到shiroFilter中##################");filterChainDefinitionMap.put("/user/edit/**", "authc,perms[user:edit]");// 這里為了測試,固定寫死的值,也可以從數據庫或其他配置中讀取//什么請求對應什么攔截規則filterChainDefinitionMap.put("/login", "authc");filterChainDefinitionMap.put("/logout", "logout");filterChainDefinitionMap.put("/**", "anon");//anon 可以理解為不攔截shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);}

總結

以上是生活随笔為你收集整理的springboot+shiro:ShiroConfiguration配置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。