素材巴巴 > 程序开发 >

springboot(3):整合shiro-redis

程序开发 2023-09-11 14:56:47

源码地址 https://github.com/ostars/springboot-fast.git  分支:dev_redis

springboot shiro整合redis做二级缓存,除了ShiroConfig.java 其余配置与shiro整合echahe做二级缓存一致

项目结构

├── doc                                           // doc files.  
 ├── log                                           // log files.  
 ├── src/main
 │   ├──java/org/ostars/springbootfast             // Specific code.  
 │    ├──security                                   // shiro config
 │    ├──├──GlobalExceptionHandler                  // 未授权进入403页面
 │    ├──├──ShiroConfig                             // 注册和配置不需要shiro控制的资源和页面
 │    ├──├──ShiroRealm                              // 验证和授权
 │    ├──├──ShiroTagConfig                          // shiro标签
 ├── src/test                                      // test.  
 ├── .gitignore                                    
 ├── LICENSE                
 ├── README.md   

pom.xml

org.crazycakeshiro-redis2.4.2.1-RELEASE
 

ShiroConfig.java

package org.ostars.springbootfast.security;import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
 import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
 import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
 import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
 import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.crazycake.shiro.RedisCacheManager;
 import org.crazycake.shiro.RedisManager;
 import org.crazycake.shiro.RedisSessionDAO;import java.util.LinkedHashMap;
 import java.util.Map;/*** shiro config** @author isaac* @date 2019/10/9*/
 @Configuration
 public class ShiroConfig {@Beanpublic ShiroFilterFactoryBean shiroFilter(DefaultWebSecurityManager securityManager) {ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();shiroFilterFactoryBean.setLoginUrl("/login");shiroFilterFactoryBean.setSuccessUrl("/index");shiroFilterFactoryBean.setUnauthorizedUrl("/403");Map filterChainDefinitionMap = new LinkedHashMap<>();shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);filterChainDefinitionMap.put("/css/**", "anon");filterChainDefinitionMap.put("/js/**", "anon");filterChainDefinitionMap.put("/img/**", "anon");filterChainDefinitionMap.put("/sysLog/**", "anon");filterChainDefinitionMap.put("/**", "authc");shiroFilterFactoryBean.setSecurityManager(securityManager);return shiroFilterFactoryBean;}@Beanpublic ShiroRealm shiroRealm() {ShiroRealm shiroRealm = new ShiroRealm();shiroRealm.setCacheManager(cacheManager());return shiroRealm;}@Beanpublic DefaultWebSecurityManager securityManager() {DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();securityManager.setRealm(shiroRealm());securityManager.setCacheManager(cacheManager());securityManager.setSessionManager(sessionManager());return securityManager;}@Bean@ConditionalOnMissingBeanpublic DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);return defaultAdvisorAutoProxyCreator;}@Beanpublic AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager) {AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor =new AuthorizationAttributeSourceAdvisor();authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);return authorizationAttributeSourceAdvisor;}public RedisCacheManager cacheManager() {RedisCacheManager redisCacheManager = new RedisCacheManager();redisCacheManager.setRedisManager(redisManager());return redisCacheManager;}public RedisManager redisManager() {RedisManager redisManager = new RedisManager();redisManager.setHost("127.0.0.1");redisManager.setPort(6379);redisManager.setExpire(1800);redisManager.setTimeout(0);return redisManager;}@Beanpublic RedisSessionDAO redisSessionDAO() {RedisSessionDAO redisSessionDAO = new RedisSessionDAO();redisSessionDAO.setRedisManager(redisManager());return redisSessionDAO;}@Beanpublic DefaultWebSessionManager sessionManager(){DefaultWebSessionManager sessionManager=new DefaultWebSessionManager();sessionManager.setSessionDAO(redisSessionDAO());return sessionManager;}}
 

 


标签:

上一篇: 在Unity中连接RabbitMQ 下一篇:
素材巴巴 Copyright © 2013-2021 http://www.sucaibaba.com/. Some Rights Reserved. 备案号:备案中。