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 ...
随机推荐
- Linux内核源码情景分析-wait()、schedule()
父进程执行wait4,并调用schedule切换到子进程: wait4(child, NULL, 0, NULL); 像其它系统调用一样.wait4()在内核中的入口是sys_wait4().代码例如 ...
- hdu 5077 NAND(打表)2014 Asia regional 鞍山站 H题
题目链接:点击打开链接 题意:就是一个按位运算的一个函数.问最少经过多少步运算能够得到给定数. 思路:不是我投机取巧想打表.是特么这题仅仅能打表.. .打表思想用能够得到的数的集合表示状态bfs:最后 ...
- 剑指offer面试题26-复杂链表的复制
题目: 请实现函数ComplexListNode* Clone(ComplexListNode* pHead).复制一个复杂链表. 在复杂链表中.每个节点除了一个m_pNext指针指向下一个节点外,另 ...
- hpc-ai比赛相关资料
http://follitude.com/blog/Use-RDMA-Emulation-in-Software-Using-SoftiWARP/ https://www.reflectionsoft ...
- FPGA中亚稳态——让你无处可逃
1. 应用背景 1.1 亚稳态发生原因 在FPGA系统中,如果数据传输中不满足触发器的Tsu和Th不满足,或者复位过程中复位信号的释放相对于有效时钟沿的恢复时间(recovery ti ...
- 关于如何让cell一直保持选中?
在M上 1.cell的展示,一直都是依靠数据源的支持.所以,必须要在数据源里面新增bool,默认为false 在V上 2.cell的setModel方法里面,将数据源的新增bool赋值为cell的是否 ...
- Java文件(io)编程——简易记事本开发
public class NotePad extends JFrame implements ActionListener{ //定义需要的组件 JTextArea jta=null; //多行文本框 ...
- 安装Windows服务方法
用sc create 服务名 binPath="路径",不要用老方法InstallUtil会出现一堆的错误
- javascript 精确加减乘除
最近一个项目中要使用 JS 实现自动计算的功能,本以为只是实现简单的加.减.乘.除就可以了,于是三下五除二做完了. 正当我窃喜的时候,发现问题了... 进行一些浮点数运算时,计算结果都是让我大跌眼镜啊 ...
- VP相关
1.485模块电平,如果是集成的IC模块,则发送低电平,接收高电平: 2.阀门程序移植至PLC注意事项: 1) 阀门程序中的变量厘清,移植过程中阀门程序中的模块有些用到了,有些没用到,所以这是变量也很 ...