导入相关包(这里配合使用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"/>

  

springboot shiro配置的更多相关文章

  1. (转)Springboot+shiro配置笔记+错误小结

    springboot不像springmvc,它没有xml配置文件,那该如何配置shiro呢,其实也不难,用java代码+注解来解决这个问题.仅以此篇记录我对shiro的学习,如有对过客造成不便,实在抱 ...

  2. Springboot+shiro配置笔记+错误小结

    软件152 尹以操 springboot不像springmvc,它没有xml配置文件,那该如何配置shiro呢,其实也不难,用java代码+注解来解决这个问题.仅以此篇记录我对shiro的学习,如有对 ...

  3. Springboot+shiro配置笔记+错误小结(转)

    软件152 尹以操 springboot不像springmvc,它没有xml配置文件,那该如何配置shiro呢,其实也不难,用java代码+注解来解决这个问题.仅以此篇记录我对shiro的学习,如有对 ...

  4. SpringBoot整合Shiro 二:Shiro配置类

    环境搭建见上篇:SpringBoot整合Shiro 一:搭建环境 Shiro配置类配置 shiro的配置主要集中在 ShiroFilterFactoryBean 中 关于权限: anon:无需认证就可 ...

  5. springboot + shiro + cas4.2.7 实战

    1. 下载地址 https://github.com/apereo/cas/archive/v4.2.7.zip 2. 解压后, 用intellj idea 打开 3. 执行 gradle build ...

  6. springboot+shiro

    作者:纯洁的微笑 出处:http://www.ityouknow.com/ 这篇文章我们来学习如何使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公 ...

  7. SpringBoot+Shiro+Redis共享Session入门小栗子

    在单机版的Springboot+Shiro的基础上,这次实现共享Session. 这里没有自己写RedisManager.SessionDAO.用的 crazycake 写的开源插件 pom.xml ...

  8. SpringBoot+Shiro入门小栗子

    写一个不花里胡哨的纯粹的Springboot+Shiro的入门小栗子 效果如图: 首页:有登录注册 先注册一个,然后登陆 登录,成功自动跳转到home页 home页:通过认证之后才可以进 代码部分: ...

  9. springboot+shiro+redis(单机redis版)整合教程-续(添加动态角色权限控制)

    相关教程: 1. springboot+shiro整合教程 2. springboot+shiro+redis(单机redis版)整合教程 3. springboot+shiro+redis(集群re ...

随机推荐

  1. HDU 4390 Number Sequence (容斥原理+组合计数)

    HDU 4390 题意: 大概就是这样.不翻译了: Given a number sequence b1,b2-bn. Please count how many number sequences a ...

  2. iOS开发UI调试神器----Reveal

    做iOS的开发,UI是非常非常重要的一环.调试时我们一般用模拟器,提交前用真机做測试.用模拟器来调试UI效果尽管快捷方便,但有时仍然希望有更强大的工具来帮助分析UI,尤其是专注在UI的效果调试时.近期 ...

  3. Android 开发之集成百度地图的定位与地图展示

    app 应用中,大多数应用都具有定位功能,百度定位就成了开发人员的集成定位功能的首选,近期也在做定位功能,可是发现百度真是个大坑啊, sdk 命名更新了,相关代码却不更新,害得我花费了非常长时间来研究 ...

  4. [JZOJ 5875] [NOIP2018提高组模拟9.20] 听我说,海蜗牛 解题报告(BFS+二分)

    题目链接: http://172.16.0.132/senior/#main/show/5875 题目: 题解: 注意这题只能经过开放的港口 我们考虑用vector存下每个点不能到的点,并把并让vec ...

  5. 网线直连Window和Ubuntu

      找根网线直接连接两台电脑. 然后设置对应的网络到相同的网关和Mask下面.  检测能否ping通就可以直连了.   Ubuntu设置网络后可在IP设置界面里设置Route, 选择直连网线仅用于连接 ...

  6. Java8 方法引用与构造器引用,数组引用

    package java_8; import org.junit.Test; import java.io.PrintStream; import java.util.Comparator; impo ...

  7. luogu 2679 子串

    子串 史上最简短的一篇博客,毕竟看题解ac心疼我的kmp /* f[i][j][k][0/1]表示A的前i个,B的前j个,用到了k个子串,当前字符选或者不选. 所以f[0][0][0][0]的方案数为 ...

  8. cell的重用

    cell的重用 简单来说,就是为了节省内存,系统通过一个重用的表示进行获取重用的控件 1 定义重用的标识 NSString * reuseId = @"hero"  //这里的he ...

  9. Edge浏览器的几个创意应用

    如果你跟我一样也喜欢书法,并且也有surface.那你可以进入我的网页.我给您准备了中国书法纸.信纸.方格子.对联等模板.满足您打发时间,精心抄佛经.诗歌,练书法等.开启Edge浏览器,开启涂鸦模式, ...

  10. 向 wmware workstation pro 的 MS-DOS 操作系统中导入文件(masm debug edit)(详细图解)

    一般MS-DOS中不包含masm.debug和edit这三个程序. 我们想要把这几个文件导入到wmware workstation pro中的DOS虚拟机中怎么做呢? [尝试] 1.我试过用共享文件夹 ...