Code Bye

有没有哪位遇到过在spring boot下集成redis后,再集成shiro后,redis使用的@Cachea

各位高手好:
咨询一个关于spring boot 的问题,这段时间本人在做spring boot的集成,在集成redis后,使用@Cacheable缓存注解都没有问题,但是就是在集成完成shiro后,@Cacheable就不起作用了,本人怀疑是原因是shiro中ehcache和redis的配置的原因,还希望各位高手指导下。代码附上:
application中相关配置:
@SpringBootApplication
@EnableTransactionManagement
@EnableJms
public class Application extends SpringBootServletInitializer {
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(Application.class);
	}
	public static void main(String[] args) throws Exception {
		SpringApplication.run(Application.class, args);
	}
	@Autowired
	private DataSourceProperties dataProperties;
	@Bean(destroyMethod="close")
	@Primary
	public DataSource dataSource(){
		DataSource dataSource=new DataSource();
		dataSource.setDriverClassName(this.dataProperties.getDriverClassName());
		dataSource.setUrl(this.dataProperties.getUrl());
		dataSource.setUsername(this.dataProperties.getUsername());
		dataSource.setPassword(this.dataProperties.getPassword());
		dataSource.setMaxActive(this.dataProperties.getMaxActive());
		dataSource.setMaxIdle(this.dataProperties.getMaxIdle());
		dataSource.setMinIdle(this.dataProperties.getMinIdle());
		dataSource.setDefaultAutoCommit(this.dataProperties.isDefaultAutoCommit());
		return dataSource;
	}
	/**
	 * 统一处理编码问题
	 * @return
	 */
	@Bean
	public FilterRegistrationBean encodingFilter(){
		CharacterEncodingFilter filter= new CharacterEncodingFilter();
		FilterRegistrationBean registrationBean=new FilterRegistrationBean();
		registrationBean.setFilter(filter);
		registrationBean.addInitParameter("encoding","UTF-8");
		registrationBean.addInitParameter("forceEncoding", "true");
		registrationBean.setName("encodingFilter");
		registrationBean.addUrlPatterns("/*");
		return registrationBean;
	}
	/**
	 * shiro filter
	 * @return
	 */
	@Bean
	public FilterRegistrationBean delegatingFilterProxy(){
		DelegatingFilterProxy proxy=new DelegatingFilterProxy();
		proxy.setTargetFilterLifecycle(true);
		FilterRegistrationBean registrationBean=new FilterRegistrationBean();
		registrationBean.setFilter(proxy);
		registrationBean.setName("shiroFilter");
		registrationBean.addUrlPatterns("/*");
		return registrationBean;
	}
}

redis配置:

@Configuration()
@EnableCaching
public class RedisConfig extends BaseLogger {
	@Autowired
	private RedisConnectionFactory redisConnectionFactory;
	@Bean
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public RedisTemplate redisTemplate() {
		RedisTemplate redisTemplate = new RedisTemplate();
		redisTemplate.setConnectionFactory(redisConnectionFactory);
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
		redisTemplate.afterPropertiesSet();
		return redisTemplate;
	}
	@Bean(name="cacheManager")
	public RedisCacheManager redisCacheManager() {
		RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate());
		return cacheManager;
	}
}

配置文件:

# redis 配置
# REDIS (RedisProperties)
# database name
#spring.redis.database=
# server host
spring.redis.host=192.168.6.36
# server password
#spring.redis.password=
# connection port
spring.redis.port=6379
# pool settings ...
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
# name of Redis server
#spring.redis.sentinel.master=
# comma-separated list of host:port pairs
#spring.redis.sentinel.nodes=

shiro配置:

@Configuration
public class ShiroConfig {

	private static Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
	@Bean(name = "shiroDbRealm")
	public ShiroDbRealm getShiroRealm() {
		return new ShiroDbRealm();
	}

	/**
	 * 缓存配置
	 * @return
	 */
	@Bean(name = "shiroEhcacheManager")
	public EhCacheManager getEhCacheManager() {
		EhCacheManager em = new EhCacheManager();
		em.setCacheManagerConfigFile("classpath:ehcache-shiro.xml");
		return em;
	}
	@Bean
	public LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {
		return new LifecycleBeanPostProcessor();
	}
	@Bean(name = "securityManager")
	public DefaultWebSecurityManager getDefaultWebSecurityManager() {
		DefaultWebSecurityManager dwsm = new DefaultWebSecurityManager();
		dwsm.setRealm(getShiroRealm());
		dwsm.setCacheManager(getEhCacheManager());
		return dwsm;
	}
	@Bean(name = "shiroFilter")
	public ShiroFilterFactoryBean getShiroFilterFactoryBean() {
		ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
//		shiroFilterFactoryBean
//				.setSecurityManager(getDefaultWebSecurityManager());
		shiroFilterFactoryBean.setLoginUrl("/login");
		shiroFilterFactoryBean.setSuccessUrl("/");
		filterChainDefinitionMap.put("/login","authc");
		filterChainDefinitionMap.put("/static/**","anon");
		filterChainDefinitionMap.put("/**", "roles[admin]");
		shiroFilterFactoryBean
				.setFilterChainDefinitionMap(filterChainDefinitionMap);
		return shiroFilterFactoryBean;
	}
}

在shiro配置中将以下代码屏蔽掉,@Cacheable就会起作用。

//		shiroFilterFactoryBean
//				.setSecurityManager(getDefaultWebSecurityManager());
解决方案

20

本人用到 springmvc , mybatis  ,shiro,redis,没有遇到题主的情况。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明有没有哪位遇到过在spring boot下集成redis后,再集成shiro后,redis使用的@Cachea