导入相关包(这里配合使用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. 带输出參数的存储过程的定义,以及在aso.net中调用

    ALTER proc [dbo].[mp_w_RechargePortalPayPal_All] ( @PayPalOrderNo nvarchar(50), --订单号 @nAccountIDFro ...

  2. Residual Networks &lt;2015 ICCV, ImageNet 图像分类Top1&gt;

    本文介绍一下2015 ImageNet中分类任务的冠军--MSRA何凯明团队的Residual Networks.实际上.MSRA是今年Imagenet的大赢家.不单在分类任务,MSRA还用resid ...

  3. Codeforces Round #261 (Div. 2)459A. Pashmak and Garden(数学题)

    题目链接:http://codeforces.com/problemset/problem/459/A A. Pashmak and Garden time limit per test 1 seco ...

  4. gson的安装和使用

    gson的安装和使用 1.安装 2.布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayo ...

  5. Kali linux 2016.2(Rolling)中的Metasploit如何更新与目录结构初步认识

    如何更新MSF 1.Windows平台 方法1: 运行msfupdate.bat 在msfconsole里执行命令svn update 或者 方法2:  2.unix/linux平台 方法1: 运行m ...

  6. Linux基础01

    ** 一些老生常谈的问题 一提起Linux,行业内无人不知<鸟哥私房菜>,就是放在胸口可以防弹的那种书,虽说经典.全面,但对于初学者而言,确实过于厚重,而且容易学着后边忘了前边,毕竟实际操 ...

  7. HDU 3342 Legal or Not【拓扑排序】

    题意:给出n,m,人的编号为 0到n-1,再给出m个关系,问能不能够进行拓扑排序 #include<iostream> #include<cstdio> #include< ...

  8. C语言基础 (3) C语言介绍

    01回顾 02 语言介绍 语言是人和人交流,C语言是人和机器交流. 03_为什么学C语言 04_第一个C代码编译运行 #include <stdio.h> int main() { // ...

  9. 安装 glusterfs yum源报错

    yum install glusterfs-server yum 一直报错 把/etc/yum.repos.d 备份 删除了所有文件,从测试机192..168.59.128上同步过来 一直报错 已加载 ...

  10. c进程学习日志

    #include<unistd.h> #include<sys/types.h> #include<pwd.h> #include<stdio.h> i ...