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"/>
springboot shiro配置的更多相关文章
- (转)Springboot+shiro配置笔记+错误小结
springboot不像springmvc,它没有xml配置文件,那该如何配置shiro呢,其实也不难,用java代码+注解来解决这个问题.仅以此篇记录我对shiro的学习,如有对过客造成不便,实在抱 ...
- Springboot+shiro配置笔记+错误小结
软件152 尹以操 springboot不像springmvc,它没有xml配置文件,那该如何配置shiro呢,其实也不难,用java代码+注解来解决这个问题.仅以此篇记录我对shiro的学习,如有对 ...
- Springboot+shiro配置笔记+错误小结(转)
软件152 尹以操 springboot不像springmvc,它没有xml配置文件,那该如何配置shiro呢,其实也不难,用java代码+注解来解决这个问题.仅以此篇记录我对shiro的学习,如有对 ...
- SpringBoot整合Shiro 二:Shiro配置类
环境搭建见上篇:SpringBoot整合Shiro 一:搭建环境 Shiro配置类配置 shiro的配置主要集中在 ShiroFilterFactoryBean 中 关于权限: anon:无需认证就可 ...
- springboot + shiro + cas4.2.7 实战
1. 下载地址 https://github.com/apereo/cas/archive/v4.2.7.zip 2. 解压后, 用intellj idea 打开 3. 执行 gradle build ...
- springboot+shiro
作者:纯洁的微笑 出处:http://www.ityouknow.com/ 这篇文章我们来学习如何使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公 ...
- SpringBoot+Shiro+Redis共享Session入门小栗子
在单机版的Springboot+Shiro的基础上,这次实现共享Session. 这里没有自己写RedisManager.SessionDAO.用的 crazycake 写的开源插件 pom.xml ...
- SpringBoot+Shiro入门小栗子
写一个不花里胡哨的纯粹的Springboot+Shiro的入门小栗子 效果如图: 首页:有登录注册 先注册一个,然后登陆 登录,成功自动跳转到home页 home页:通过认证之后才可以进 代码部分: ...
- springboot+shiro+redis(单机redis版)整合教程-续(添加动态角色权限控制)
相关教程: 1. springboot+shiro整合教程 2. springboot+shiro+redis(单机redis版)整合教程 3. springboot+shiro+redis(集群re ...
随机推荐
- windows编译ffmpeg出现gcc is unable to create an executable file 的普通情况
近期有个朋友在编译ffmpeg的时候出现这个问题,他非常郁闷. 我就说,为什么我弄的时候就没问题呢??直接./configure +加上后面的參数 安全度过. 然后,我就想了,预计他的gcc的系统变量 ...
- ios+openflow 问题
环境:xcode5.1+ios7.1 需求:A试图 的scroll加入 B视图:[A addSubview:B.view] 问题: 1.B视图载入到A视图上了,但Openflow的图片未载入.后经调试 ...
- 2 怎样解析XML文件或字符串
1 引用XML文件 2 使用XMLReader解析文本字符串 3 使用XMLReader方法读取XML数据 详细代码实现例如以下: //初始化一个XML字符串 String xmlString = @ ...
- 数组溢界地址的正确使用: 即 int a[6] 中的 a[-1] 和 a[6] 正确使用
正如大家所知道的那样: 数组 int a[6] , 编译器阅读到这句数组定义,会为分配6个int 类型的地址:a[0] a[1] a[2] a[3] a[4] a[5].我们 能够正 ...
- C++语言笔记系列之十八——虚函数(1)
1.C++中的多态 (1)多态性:同一个函数的调用能够进行不同的操作,函数重载是实现多态的一种手段. (2)联编:在编译阶段进行联接.即是在编译阶段将一个函数的调用点和函数的定义点联接起来. A.静态 ...
- 通过setInterval函数在地图上每隔1s打一次点
<?php echo <<<_END <!doctype html> <html> <head> <meta charset=&quo ...
- Wireshark filter语法
过滤器语法 ------------------------------------------------------------- 最简单的过滤允许你检查一个协议或者字段的存在.如果你想查看所有的 ...
- JS的数据类型(包含:7种数据类型的介绍、数据类型的转换、数据类型的判断)
前言 最新的 ECMAScript 标准定义了JS的 7 种数据类型,其中包括: 6 种基本类型:Boolean.Null.Undefined.Number.String.Symbol (ECMASc ...
- (转载) android快速搭建项目积累
android快速搭建项目积累 2016-04-05 20:07 519人阅读 评论(0) 收藏 举报 分类: android优化(8) Rx技术(5) 版权声明:本文为博主原创文章,未经博主 ...
- HitHub使用
GitHub是个免费的开源仓库,个人及组织可以将源代码上传,既可以让别人参与到自己的项目中,也可以学习与参与到他人的项目中去.免费的GitHub账号的代码仓库(repository)都是公开的,任何人 ...