springboot shiro配置


導入相關包(這里配合使用Ehcache緩存)

        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-ehcache</artifactId>
            <version>1.3.2</version>
        </dependency>

  

添加配置文件類(注意啟動類的掃描范圍,可自定義)

@Configuration
public class ShiroConfig {

    @Autowired
    EhCacheManagerFactoryBean ehCacheManagerFactoryBean;

    /**
     * 開啟Shiro的注解(如@RequiresRoles,@RequiresPermissions),需借助SpringAOP掃描使用Shiro注解的類,並在必要時進行安全邏輯驗證
     * 配置以下兩個bean(DefaultAdvisorAutoProxyCreator(可選)和AuthorizationAttributeSourceAdvisor)即可實現此功能
     *
     * @return
     */
    @Bean
    public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        advisorAutoProxyCreator.setProxyTargetClass(true);
        return advisorAutoProxyCreator;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor() {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager());
        return authorizationAttributeSourceAdvisor;
    }

// 解決shiroFilter無法注入bean的問題 @Bean public FilterRegistrationBean delegatingFilterProxy() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); DelegatingFilterProxy proxy = new DelegatingFilterProxy(); proxy.setTargetFilterLifecycle(true); proxy.setTargetBeanName("shiroFilter"); filterRegistrationBean.setFilter(proxy); return filterRegistrationBean; } @Bean("shiroFilter") public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) { ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); shiroFilterFactoryBean.setSecurityManager(securityManager); Map<String, Filter> filters = new HashMap<>(); filters.put("rbacFilter", new RBACPermissionFilter()); // 自定義攔截類 shiroFilterFactoryBean.setFilters(filters); //攔截器. Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>(); filterChainDefinitionMap.put("*.do", "rbacFilter"); shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); return shiroFilterFactoryBean; } @Bean public SecurityManager securityManager() { DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); securityManager.setCacheManager(myShiroCacheManager()); securityManager.setRealm(myShiroRealm()); securityManager.setSessionManager(myShiroSession()); return securityManager; } @Bean public SessionManager myShiroSession() { DefaultWebSessionManager sessionManager = new DefaultWebSessionManager(); sessionManager.setDeleteInvalidSessions(true); sessionManager.setSessionIdCookie(myShiroCookie()); sessionManager.setCacheManager(myShiroCacheManager()); sessionManager.setSessionDAO(mySessionDao()); sessionManager.setSessionValidationInterval(7200000L); sessionManager.setSessionValidationSchedulerEnabled(true); sessionManager.setSessionValidationScheduler(mySessionScheduler()); sessionManager.setSessionIdUrlRewritingEnabled(false); return sessionManager; } @Bean public EhCacheManager myShiroCacheManager() { EhCacheManager ehCacheManager = new EhCacheManager(); ehCacheManager.setCacheManager(ehCacheManagerFactoryBean.getObject()); // 添加ehcache緩存 詳細見上文章 return ehCacheManager; } @Bean public SimpleCookie myShiroCookie() { SimpleCookie simpleCookie = new SimpleCookie("rsId"); // session的JSESSIONID simpleCookie.setPath("/"); simpleCookie.setHttpOnly(true); simpleCookie.setMaxAge(7200); return simpleCookie; } @Bean public SessionValidationScheduler mySessionScheduler() { ExecutorServiceSessionValidationScheduler executorServiceSessionValidationScheduler = new ExecutorServiceSessionValidationScheduler(); executorServiceSessionValidationScheduler.setInterval(7200000L); return executorServiceSessionValidationScheduler; } @Bean public SessionDAO mySessionDao() { EnterpriseCacheSessionDAO enterpriseCacheSessionDAO = new EnterpriseCacheSessionDAO(); enterpriseCacheSessionDAO.setCacheManager(myShiroCacheManager()); enterpriseCacheSessionDAO.setActiveSessionsCacheName("shiro-activeSessionCache"); // 緩存name return enterpriseCacheSessionDAO; }
// 自定義realm類 @Bean public MyShiroRealm myShiroRealm() { MyShiroRealm myShiroRealm = new MyShiroRealm(); myShiroRealm.setCacheManager(myShiroCacheManager()); myShiroRealm.setAuthenticationCacheName("shiroDbRealm.authorizationCache"); return myShiroRealm; } }

  

    <!-- Shiro Cache Config -->
    <cache name="shiroDbRealm.authorizationCache"
           maxElementsInMemory="200000"
           eternal="true"
           diskPersistent="false"
           overflowToDisk="true"
           diskExpiryThreadIntervalSeconds="120">
    </cache>
    <cache name="shiro-activeSessionCache"
           maxElementsInMemory="1"
           memoryStoreEvictionPolicy="FIFO"
           eternal="true"
           diskPersistent="true"
           overflowToDisk="true"
           maxElementsOnDisk="0"
           diskExpiryThreadIntervalSeconds="120"/>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM