package com.example.shirodemo.config;
import com.example.shirodemo.filter.MyFilter;
import net.sf.ehcache.CacheManager;
import org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;
@Configuration
public class ShiroConfig {
@Bean(name = "shiroFilter")
public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager) {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
//注入核心安全管理器
shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
//注入拦截器
Map<String, Filter> filters = new LinkedHashMap<>();
filters.put("authc", new MyFilter());
shiroFilterFactoryBean.setFilters(filters);
//配置拦截链
Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
filterChainDefinitionMap.put("/user/add", "authc");
filterChainDefinitionMap.put("/user/update", "authc");
//设置登录的请求
shiroFilterFactoryBean.setLoginUrl("/login");
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
return shiroFilterFactoryBean;
}
@Bean(name = "securityManager")
public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm) {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setRealm(userRealm);
securityManager.setSessionManager(sessionManager());
//securityManager.setCacheManager(cacheManager());
return securityManager;
}
/**
* 会话管理
* 默认使用容器session,这里改为自定义session
* session的全局超时时间默认是30分钟
*
* @return
*/
@Bean
public DefaultWebSessionManager sessionManager() {
DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
// sessionId cookie
SimpleCookie cookie = new SimpleCookie();
cookie.setName("sid");
// 设置cookie的存活时间为30分钟,与session timeout时间一致
cookie.setMaxAge(1800);
cookie.setHttpOnly(true);
sessionManager.setSessionIdCookie(cookie);
sessionManager.setSessionIdCookieEnabled(true);
//默认使用MemerySessionDao,设置为EnterpriseCacheSessionDAO以配合ehcache实现分布式集群缓存支持
sessionManager.setSessionDAO(new EnterpriseCacheSessionDAO());
return sessionManager;
}
/**
* 缓存管理器
* 配合session dao实现分布式集群session,用于进程内缓存session
*
* @return
*/
// @Bean(name = "ehCacheManager")
// public CacheManager cacheManager(EhCacheManagerFactoryBean bean) {
// return bean.getObject();
// }
// @Bean
// public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {
// EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean();
// cacheManagerFactoryBean.setConfigLocation(new ClassPathResource("myEhcache.xml"));
// cacheManagerFactoryBean.setShared(true);
// // 设置完属性后,cacheManagerFactoryBean会执行afterProertiesSet()方法,
// // 所以不能在这里直接执行cacheManagerFactoryBean.getObject(),直接执行的话,因为在afterPropertiesSet()方法之前执行,
// // 所以:getObject()会得到null值
// return cacheManagerFactoryBean;
// }
//创建realm对象,需要自定义
@Bean
public UserRealm userRealm() {
return new UserRealm();
}
}