packagecom.ruoyi.framework.config;importat.pollux.thymeleaf.shiro.dialect.ShiroDialect;importcom.ruoyi.common.utils.StringUtils;importcom.ruoyi.framework.shiro.realm.UserRealm;importcom.ruoyi.framework.shiro.session.OnlineSessionDAO;importcom.ruoyi.framework.shiro.session.OnlineSessionFactory;importcom.ruoyi.framework.shiro.web.filter.LogoutFilter;importcom.ruoyi.framework.shiro.web.filter.captcha.CaptchaValidateFilter;importcom.ruoyi.framework.shiro.web.filter.online.OnlineSessionFilter;importcom.ruoyi.framework.shiro.web.filter.sync.SyncOnlineSessionFilter;importcom.ruoyi.framework.shiro.web.session.OnlineWebSessionManager;importcom.ruoyi.framework.shiro.web.session.SpringSessionValidationScheduler;importorg.apache.shiro.cache.ehcache.EhCacheManager;importorg.apache.shiro.codec.Base64;importorg.apache.shiro.mgt.SecurityManager;importorg.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;importorg.apache.shiro.spring.web.ShiroFilterFactoryBean;importorg.apache.shiro.web.mgt.CookieRememberMeManager;importorg.apache.shiro.web.mgt.DefaultWebSecurityManager;importorg.apache.shiro.web.servlet.SimpleCookie;importorg.springframework.beans.factory.annotation.Qualifier;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importjavax.servlet.Filter;importjava.util.LinkedHashMap;importjava.util.Map;/*** 权限配置加载
*
*@authorruoyi*/@Configurationpublic classShiroConfig
{public static final String PREMISSION_STRING = "perms[\"{0}\"]";//Session超时时间,单位为毫秒(默认30分钟)
@Value("${shiro.session.expireTime}")private intexpireTime;//相隔多久检查一次session的有效性,单位毫秒,默认就是10分钟
@Value("${shiro.session.validationInterval}")private intvalidationInterval;//验证码开关
@Value("${shiro.user.captchaEnabled}")private booleancaptchaEnabled;//验证码类型
@Value("${shiro.user.captchaType}")privateString captchaType;//设置Cookie的域名
@Value("${shiro.cookie.domain}")privateString domain;//设置cookie的有效访问路径
@Value("${shiro.cookie.path}")privateString path;//设置HttpOnly属性
@Value("${shiro.cookie.httpOnly}")private booleanhttpOnly;//设置Cookie的过期时间,秒为单位
@Value("${shiro.cookie.maxAge}")private intmaxAge;//登录地址
@Value("${shiro.user.loginUrl}")privateString loginUrl;//权限认证失败地址
@Value("${shiro.user.unauthorizedUrl}")privateString unauthorizedUrl;/*** 缓存管理器 使用Ehcache实现*/@BeanpublicEhCacheManager getEhCacheManager()
{
net.sf.ehcache.CacheManager cacheManager= net.sf.ehcache.CacheManager.getCacheManager("ruoyi");
EhCacheManager em= newEhCacheManager();if(StringUtils.isNull(cacheManager))
{
em.setCacheManagerConfigFile("classpath:ehcache/ehcache-shiro.xml");returnem;
}else{
em.setCacheManager(cacheManager);returnem;
}
}/*** 自定义Realm*/@BeanpublicUserRealm userRealm(EhCacheManager cacheManager)
{
UserRealm userRealm= newUserRealm();
userRealm.setCacheManager(cacheManager);returnuserRealm;
}/*** 自定义sessionDAO会话*/@BeanpublicOnlineSessionDAO sessionDAO()
{
OnlineSessionDAO sessionDAO= newOnlineSessionDAO();returnsessionDAO;
}/*** 自定义sessionFactory会话*/@BeanpublicOnlineSessionFactory sessionFactory()
{
OnlineSessionFactory sessionFactory= newOnlineSessionFactory();returnsessionFactory;
}/*** 自定义sessionFactory调度器*/@BeanpublicSpringSessionValidationScheduler sessionValidationScheduler()
{
SpringSessionValidationScheduler sessionValidationScheduler= newSpringSessionValidationScheduler();//相隔多久检查一次session的有效性,单位毫秒,默认就是10分钟
sessionValidationScheduler.setSessionValidationInterval(validationInterval * 60 * 1000);//设置会话验证调度器进行会话验证时的会话管理器
sessionValidationScheduler.setSessionManager(sessionValidationManager());returnsessionValidationScheduler;
}/*** 会话管理器*/@BeanpublicOnlineWebSessionManager sessionValidationManager()
{
OnlineWebSessionManager manager= newOnlineWebSessionManager();//加入缓存管理器
manager.setCacheManager(getEhCacheManager());//删除过期的session
manager.setDeleteInvalidSessions(true);//设置全局session超时时间
manager.setGlobalSessionTimeout(expireTime * 60 * 1000);//去掉 JSESSIONID
manager.setSessionIdUrlRewritingEnabled(false);//是否定时检查session
manager.setSessionValidationSchedulerEnabled(true);//自定义SessionDao
manager.setSessionDAO(sessionDAO());//自定义sessionFactory
manager.setSessionFactory(sessionFactory());returnmanager;
}/*** 会话管理器*/@BeanpublicOnlineWebSessionManager sessionManager()
{
OnlineWebSessionManager manager= newOnlineWebSessionManager();//加入缓存管理器
manager.setCacheManager(getEhCacheManager());//删除过期的session
manager.setDeleteInvalidSessions(true);//设置全局session超时时间
manager.setGlobalSessionTimeout(expireTime * 60 * 1000);//去掉 JSESSIONID
manager.setSessionIdUrlRewritingEnabled(false);//定义要使用的无效的Session定时调度器
manager.setSessionValidationScheduler(sessionValidationScheduler());//是否定时检查session
manager.setSessionValidationSchedulerEnabled(true);//自定义SessionDao
manager.setSessionDAO(sessionDAO());//自定义sessionFactory
manager.setSessionFactory(sessionFactory());returnmanager;
}/*** 安全管理器*/@BeanpublicSecurityManager securityManager(UserRealm userRealm)
{
DefaultWebSecurityManager securityManager= newDefaultWebSecurityManager();//设置realm.
securityManager.setRealm(userRealm);//记住我
securityManager.setRememberMeManager(rememberMeManager());//注入缓存管理器;
securityManager.setCacheManager(getEhCacheManager());//session管理器
securityManager.setSessionManager(sessionManager());returnsecurityManager;
}/*** 退出过滤器*/
publicLogoutFilter logoutFilter()
{
LogoutFilter logoutFilter= newLogoutFilter();
logoutFilter.setLoginUrl(loginUrl);returnlogoutFilter;
}/*** Shiro过滤器配置*/@BeanpublicShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager)
{
ShiroFilterFactoryBean shiroFilterFactoryBean= newShiroFilterFactoryBean();//Shiro的核心安全接口,这个属性是必须的
shiroFilterFactoryBean.setSecurityManager(securityManager);//身份认证失败,则跳转到登录页面的配置
shiroFilterFactoryBean.setLoginUrl(loginUrl);//权限认证失败,则跳转到指定页面
shiroFilterFactoryBean.setUnauthorizedUrl(unauthorizedUrl);//Shiro连接约束配置,即过滤链的定义
LinkedHashMap filterChainDefinitionMap = new LinkedHashMap<>();//对静态资源设置匿名访问
filterChainDefinitionMap.put("/favicon.ico**", "anon");//uflo工作流不拦截
filterChainDefinitionMap.put("/uflo/**", "anon");
filterChainDefinitionMap.put("/ruoyi.png**", "anon");
filterChainDefinitionMap.put("/css/**", "anon");
filterChainDefinitionMap.put("/docs/**", "anon");
filterChainDefinitionMap.put("/fonts/**", "anon");
filterChainDefinitionMap.put("/img/**", "anon");
filterChainDefinitionMap.put("/ajax/**", "anon");
filterChainDefinitionMap.put("/js/**", "anon");
filterChainDefinitionMap.put("/ruoyi/**", "anon");
filterChainDefinitionMap.put("/druid/**", "anon");
filterChainDefinitionMap.put("/captcha/captchaImage**", "anon");//退出 logout地址,shiro去清除session
filterChainDefinitionMap.put("/logout", "logout");//不需要拦截的访问
filterChainDefinitionMap.put("/login", "anon,captchaValidate");//系统权限列表//filterChainDefinitionMap.putAll(SpringUtils.getBean(IMenuService.class).selectPermsAll());
Map filters = new LinkedHashMap<>();
filters.put("onlineSession", onlineSessionFilter());
filters.put("syncOnlineSession", syncOnlineSessionFilter());
filters.put("captchaValidate", captchaValidateFilter());//注销成功,则跳转到指定页面
filters.put("logout", logoutFilter());
shiroFilterFactoryBean.setFilters(filters);//所有请求需要认证
filterChainDefinitionMap.put("/**", "user,onlineSession,syncOnlineSession");
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);returnshiroFilterFactoryBean;
}/*** 自定义在线用户处理过滤器*/@BeanpublicOnlineSessionFilter onlineSessionFilter()
{
OnlineSessionFilter onlineSessionFilter= newOnlineSessionFilter();
onlineSessionFilter.setLoginUrl(loginUrl);returnonlineSessionFilter;
}/*** 自定义在线用户同步过滤器*/@BeanpublicSyncOnlineSessionFilter syncOnlineSessionFilter()
{
SyncOnlineSessionFilter syncOnlineSessionFilter= newSyncOnlineSessionFilter();returnsyncOnlineSessionFilter;
}/*** 自定义验证码过滤器*/@BeanpublicCaptchaValidateFilter captchaValidateFilter()
{
CaptchaValidateFilter captchaValidateFilter= newCaptchaValidateFilter();
captchaValidateFilter.setCaptchaEnabled(captchaEnabled);
captchaValidateFilter.setCaptchaType(captchaType);returncaptchaValidateFilter;
}/*** cookie 属性设置*/
publicSimpleCookie rememberMeCookie()
{
SimpleCookie cookie= new SimpleCookie("rememberMe");
cookie.setDomain(domain);
cookie.setPath(path);
cookie.setHttpOnly(httpOnly);
cookie.setMaxAge(maxAge* 24 * 60 * 60);returncookie;
}/*** 记住我*/
publicCookieRememberMeManager rememberMeManager()
{
CookieRememberMeManager cookieRememberMeManager= newCookieRememberMeManager();
cookieRememberMeManager.setCookie(rememberMeCookie());
cookieRememberMeManager.setCipherKey(Base64.decode("fCq+/xW488hMTCD+cmJ3aQ=="));returncookieRememberMeManager;
}/*** thymeleaf模板引擎和shiro框架的整合*/@BeanpublicShiroDialect shiroDialect()
{return newShiroDialect();
}/*** 开启Shiro注解通知器*/@BeanpublicAuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(
@Qualifier("securityManager") SecurityManager securityManager)
{
AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor= newAuthorizationAttributeSourceAdvisor();
authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);returnauthorizationAttributeSourceAdvisor;
}
}