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"/>

  

转载于:https://www.cnblogs.com/skyLogin/p/9233070.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> 02 <property name="securityManager" ref="securityManager"/> 03 <!-- override these for application-specific URLs if you like:--> 04 <!-- <property name="loginUrl" value="/welcome" />--> 05 <!-- <property name="successUrl" value="/blog/drug/list"/>--> 06 <property name="unauthorizedUrl" value="/unauthorized.jsp"/> 07 <!-- The 'filters' property is not necessary since any declared javax.servlet.Filter bean --> 08 <!-- defined will be automatically acquired and available via its beanName in chain --> 09 <!-- definitions, but you can perform instance overrides or name aliases here if you like: --> 10 <!-- <property name="filters"> 11 <util:map> 12 <entry key="anAlias" value-ref="someFilter"/> 13 </util:map> 14 </property> --> 15 <property name="filterChainDefinitions"> 16 <value> 17 /login* = anon 18 /welcome = anon 19 /slave4j/** = anon 20 /list = anon 21 /** = authc 22 </value> 23 </property> 24 </bean> 25 26 <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> 27 28 <!-- 基于ehCache来缓存用户认证信息和授权信息的实现 --> 29 <property name="cacheManager" ref="shiroCacheManager"/> 30 31 <!-- Single realm app. If you have multiple realms, use the 'realms' property instead. --> 32 <property name="realm" ref="jdbcRealm"/> 33 34 <!-- sessionMode参数设置为native时,那么shrio就将用户的基本认证信息保存到缺省名称为shiro-activeSessionCacheCache中 --> 35 <property name="sessionMode" value="native" /> 36 37 <!-- 缺省使用的是DefaultWebSessionManager来管理Session,该管理类缺省使用的是使用MemorySessionDAO基于内存来保存和操作用户基本认证信息。如果系统内的用户数特别多,我们需要使用CacheSessionDao来基于Cache进行操作 --> 38 <property name="sessionManager" ref="shiroSessionManager"/> 39 </bean> 40 41 <!-- jdbcRealm --> 42 <bean id="jdbcRealm" class="org.slave4j.shiro.MyRealm"> 43 <!-- shiro.authorizationCache是用来存放授权信息的Cache --> 44 <property name="authorizationCacheName" value="shiro.authorizationCache"/> 45 </bean> 46 47 <!-- shiroSessionManager --> 48 <bean id="shiroSessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager"> 49 <property name="sessionDAO" ref="sessionDAO"/> 50 <property name="sessionListeners"> 51 <set><bean class="org.slave4j.shiro.SessionHandler"/> </set> 52 </property> 53 54 <property name="sessionValidationScheduler" ref="sessionValidationScheduler" /> 55 <property name="globalSessionTimeout" value="180000"/> 56 <property name="sessionValidationSchedulerEnabled" value="true"/> 57 <property name="deleteInvalidSessions" value="true"/> 58 </bean> 59 <bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO"> 60 <property name="activeSessionsCacheName" value="shiro-activeSessionCache"/> 61 </bean> 62 <bean id="sessionValidationScheduler" class="org.apache.shiro.session.mgt.ExecutorServiceSessionValidationScheduler"> 63 <property name="interval" value="5000"/> 64 </bean> 65 66 <!-- shiroCacheManager --> 67 <!-- Let's use some enterprise caching support for better performance. You can replace this with any enterprise 68 caching framework implementation that you like (Terracotta+Ehcache, Coherence, GigaSpaces, etc --> 69 <bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> 70 <!-- Set a net.sf.ehcache.CacheManager instance here if you already have one. If not, a new one 71 will be created with a default config: --> 72 <property name="cacheManager" ref="ehCacheManager"/> 73 <!-- If you don't have a pre-built net.sf.ehcache.CacheManager instance to inject, but you want 74 a specific Ehcache configuration to be used, specify that here. If you don't, a default 75 will be used.:--> 76 <property name="cacheManagerConfigFile" value="classpath:shiro_ehcache.xml"/> 77 </bean> 78 <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"/> 79 80 <!-- Enable Shiro Annotations for Spring-configured beans. Only run after the lifecycleBeanProcessor has run: --> 81 <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> 82 <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> 83 <property name="securityManager" ref="securityManager"/> 84 </bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值