1 changed files with 285 additions and 284 deletions
@ -1,284 +1,285 @@ |
|||||
package com.zdxt.common.service; |
package com.zdxt.common.service; |
||||
|
|
||||
import at.pollux.thymeleaf.shiro.dialect.ShiroDialect; |
import at.pollux.thymeleaf.shiro.dialect.ShiroDialect; |
||||
|
|
||||
import org.apache.commons.lang.StringUtils; |
import org.apache.commons.lang.StringUtils; |
||||
import org.apache.shiro.codec.Base64; |
import org.apache.shiro.codec.Base64; |
||||
import org.apache.shiro.mgt.SecurityManager; |
import org.apache.shiro.mgt.SecurityManager; |
||||
import org.apache.shiro.session.mgt.SessionManager; |
import org.apache.shiro.session.mgt.SessionManager; |
||||
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; |
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; |
||||
import org.apache.shiro.spring.web.ShiroFilterFactoryBean; |
import org.apache.shiro.spring.web.ShiroFilterFactoryBean; |
||||
import org.apache.shiro.web.mgt.CookieRememberMeManager; |
import org.apache.shiro.web.mgt.CookieRememberMeManager; |
||||
import org.apache.shiro.web.mgt.DefaultWebSecurityManager; |
import org.apache.shiro.web.mgt.DefaultWebSecurityManager; |
||||
import org.apache.shiro.web.servlet.SimpleCookie; |
import org.apache.shiro.web.servlet.SimpleCookie; |
||||
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager; |
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager; |
||||
import org.crazycake.shiro.RedisCacheManager; |
import org.crazycake.shiro.RedisCacheManager; |
||||
import org.crazycake.shiro.RedisManager; |
import org.crazycake.shiro.RedisManager; |
||||
import org.crazycake.shiro.RedisSessionDAO; |
import org.crazycake.shiro.RedisSessionDAO; |
||||
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator; |
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator; |
||||
import org.springframework.beans.factory.annotation.Value; |
import org.springframework.beans.factory.annotation.Value; |
||||
import org.springframework.context.annotation.Bean; |
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.context.annotation.Profile; |
import org.springframework.context.annotation.Profile; |
||||
|
|
||||
import javax.servlet.Filter; |
import javax.servlet.Filter; |
||||
import java.util.LinkedHashMap; |
import java.util.LinkedHashMap; |
||||
import java.util.Map; |
import java.util.Map; |
||||
|
|
||||
/** |
/** |
||||
* 权限配置加载 |
* 权限配置加载 |
||||
* |
* |
||||
* @author ruoyi |
* @author ruoyi |
||||
*/ |
*/ |
||||
// 启用cas环境时不生效,环境切换在application.yml中配置
|
// 启用cas环境时不生效,环境切换在application.yml中配置
|
||||
@Profile("!cas") |
@Profile("!cas") |
||||
@Configuration |
@Configuration |
||||
public class ShiroConfig { |
public class ShiroConfig { |
||||
public static final String PREMISSION_STRING = "perms[\"{0}\"]"; |
public static final String PREMISSION_STRING = "perms[\"{0}\"]"; |
||||
|
|
||||
// Session超时时间,单位为毫秒(默认30分钟)
|
// Session超时时间,单位为毫秒(默认30分钟)
|
||||
@Value("${shiro.session.expireTime}") |
@Value("${shiro.session.expireTime}") |
||||
private int expireTime; |
private int expireTime; |
||||
|
|
||||
// 相隔多久检查一次session的有效性,单位毫秒,默认就是10分钟
|
// 相隔多久检查一次session的有效性,单位毫秒,默认就是10分钟
|
||||
@Value("${shiro.session.validationInterval}") |
@Value("${shiro.session.validationInterval}") |
||||
private int validationInterval; |
private int validationInterval; |
||||
|
|
||||
// 同一个用户最大会话数
|
// 同一个用户最大会话数
|
||||
@Value("${shiro.session.maxSession}") |
@Value("${shiro.session.maxSession}") |
||||
private int maxSession; |
private int maxSession; |
||||
|
|
||||
// 踢出之前登录的/之后登录的用户,默认踢出之前登录的用户
|
// 踢出之前登录的/之后登录的用户,默认踢出之前登录的用户
|
||||
@Value("${shiro.session.kickoutAfter}") |
@Value("${shiro.session.kickoutAfter}") |
||||
private boolean kickoutAfter; |
private boolean kickoutAfter; |
||||
|
|
||||
// 验证码开关
|
// 验证码开关
|
||||
@Value("${shiro.user.captchaEnabled}") |
@Value("${shiro.user.captchaEnabled}") |
||||
private boolean captchaEnabled; |
private boolean captchaEnabled; |
||||
|
|
||||
// 验证码类型
|
// 验证码类型
|
||||
@Value("${shiro.user.captchaType}") |
@Value("${shiro.user.captchaType}") |
||||
private String captchaType; |
private String captchaType; |
||||
|
|
||||
// 设置Cookie的域名
|
// 设置Cookie的域名
|
||||
@Value("${shiro.cookie.domain}") |
@Value("${shiro.cookie.domain}") |
||||
private String domain; |
private String domain; |
||||
|
|
||||
// 设置cookie的有效访问路径
|
// 设置cookie的有效访问路径
|
||||
@Value("${shiro.cookie.path}") |
@Value("${shiro.cookie.path}") |
||||
private String path; |
private String path; |
||||
|
|
||||
// 设置HttpOnly属性
|
// 设置HttpOnly属性
|
||||
@Value("${shiro.cookie.httpOnly}") |
@Value("${shiro.cookie.httpOnly}") |
||||
private boolean httpOnly; |
private boolean httpOnly; |
||||
|
|
||||
// 设置Cookie的过期时间,秒为单位
|
// 设置Cookie的过期时间,秒为单位
|
||||
@Value("${shiro.cookie.maxAge}") |
@Value("${shiro.cookie.maxAge}") |
||||
private int maxAge; |
private int maxAge; |
||||
// 设置cipherKey密钥
|
// 设置cipherKey密钥
|
||||
@Value("${shiro.cookie.cipherKey}") |
@Value("${shiro.cookie.cipherKey}") |
||||
private String cipherKey; |
private String cipherKey; |
||||
|
|
||||
// 登录地址
|
// 登录地址
|
||||
@Value("${shiro.user.loginUrl}") |
@Value("${shiro.user.loginUrl}") |
||||
private String loginUrl; |
private String loginUrl; |
||||
|
|
||||
// 权限认证失败地址
|
// 权限认证失败地址
|
||||
@Value("${shiro.user.unauthorizedUrl}") |
@Value("${shiro.user.unauthorizedUrl}") |
||||
private String unauthorizedUrl; |
private String unauthorizedUrl; |
||||
/** |
/** |
||||
* redis缓存地址 |
* redis缓存地址 |
||||
*/ |
*/ |
||||
@Value("${spring.redis.port}") |
@Value("${spring.redis.port}") |
||||
private String redisPort; |
private String redisPort; |
||||
|
|
||||
/** |
/** |
||||
* redis缓存端口 |
* redis缓存端口 |
||||
*/ |
*/ |
||||
@Value("${spring.redis.host}") |
@Value("${spring.redis.host}") |
||||
private String redisHost; |
private String redisHost; |
||||
|
|
||||
/** |
/** |
||||
* redis数据库索引 |
* redis数据库索引 |
||||
*/ |
*/ |
||||
@Value("${spring.redis.database}") |
@Value("${spring.redis.database}") |
||||
private int database; |
private int database; |
||||
|
|
||||
/** |
/** |
||||
* redis密码 |
* redis密码 |
||||
*/ |
*/ |
||||
@Value("${spring.redis.password}") |
@Value("${spring.redis.password}") |
||||
private String password; |
private String password; |
||||
/** |
/** |
||||
* Cache Manager (shiro-redis) |
* Cache Manager (shiro-redis) |
||||
*/ |
*/ |
||||
@Bean |
@Bean |
||||
public RedisCacheManager redisCacheManager() |
public RedisCacheManager redisCacheManager() |
||||
{ |
{ |
||||
RedisCacheManager redisCacheManager = new RedisCacheManager(); |
RedisCacheManager redisCacheManager = new RedisCacheManager(); |
||||
redisCacheManager.setRedisManager(redisManager()); |
redisCacheManager.setRedisManager(redisManager()); |
||||
redisCacheManager.setPrincipalIdFieldName("userId"); |
redisCacheManager.setPrincipalIdFieldName("userId"); |
||||
return redisCacheManager; |
return redisCacheManager; |
||||
} |
} |
||||
|
|
||||
/** |
/** |
||||
* RedisManager (shiro-redis) |
* RedisManager (shiro-redis) |
||||
*/ |
*/ |
||||
@Bean |
@Bean |
||||
public RedisManager redisManager() |
public RedisManager redisManager() |
||||
{ |
{ |
||||
RedisManager redisManager = new RedisManager(); |
RedisManager redisManager = new RedisManager(); |
||||
redisManager.setHost(redisHost + ":" + redisPort); |
redisManager.setHost(redisHost + ":" + redisPort); |
||||
redisManager.setDatabase(database); |
redisManager.setDatabase(database); |
||||
if (StringUtils.isNotEmpty(password)) |
if (StringUtils.isNotEmpty(password)) |
||||
{ |
{ |
||||
redisManager.setPassword(password); |
redisManager.setPassword(password); |
||||
} |
} |
||||
redisManager.setTimeout(expireTime * 60); |
redisManager.setTimeout(expireTime * 60); |
||||
return redisManager; |
return redisManager; |
||||
} |
} |
||||
/** |
/** |
||||
* 自定义Realm |
* 自定义Realm |
||||
*/ |
*/ |
||||
@Bean |
@Bean |
||||
public ShiroAuthorizingRealm userRealm() |
public ShiroAuthorizingRealm userRealm() |
||||
{ |
{ |
||||
ShiroAuthorizingRealm userRealm = new ShiroAuthorizingRealm(); |
ShiroAuthorizingRealm userRealm = new ShiroAuthorizingRealm(); |
||||
// userRealm.setAuthorizationCacheName(Constants.SYS_AUTH_CACHE);
|
// userRealm.setAuthorizationCacheName(Constants.SYS_AUTH_CACHE);
|
||||
userRealm.setCacheManager(redisCacheManager()); |
userRealm.setCacheManager(redisCacheManager()); |
||||
return userRealm; |
return userRealm; |
||||
} |
} |
||||
@Bean |
@Bean |
||||
public SecurityManager securityManager(ShiroAuthorizingRealm userRealm) |
public SecurityManager securityManager(ShiroAuthorizingRealm userRealm) |
||||
{ |
{ |
||||
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); |
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); |
||||
// 设置realm.
|
// 设置realm.
|
||||
securityManager.setRealm(userRealm); |
securityManager.setRealm(userRealm); |
||||
|
|
||||
// 注入缓存管理器;
|
// 注入缓存管理器;
|
||||
// securityManager.setCacheManager(redisCacheManager());
|
// securityManager.setCacheManager(redisCacheManager());
|
||||
// session管理器
|
// session管理器
|
||||
securityManager.setSessionManager(sessionManager()); |
securityManager.setSessionManager(sessionManager()); |
||||
return securityManager; |
return securityManager; |
||||
} |
} |
||||
/** |
/** |
||||
* RedisSessionDAO (shiro-redis) |
* RedisSessionDAO (shiro-redis) |
||||
*/ |
*/ |
||||
@Bean |
@Bean |
||||
public RedisSessionDAO redisSessionDAO() |
public RedisSessionDAO redisSessionDAO() |
||||
{ |
{ |
||||
RedisSessionDAO redisSessionDAO = new RedisSessionDAO(); |
RedisSessionDAO redisSessionDAO = new RedisSessionDAO(); |
||||
redisSessionDAO.setRedisManager(redisManager()); |
redisSessionDAO.setRedisManager(redisManager()); |
||||
redisSessionDAO.setExpire(expireTime * 60); |
redisSessionDAO.setExpire(expireTime * 60); |
||||
return redisSessionDAO; |
return redisSessionDAO; |
||||
} |
} |
||||
|
|
||||
/** |
/** |
||||
* 会话管理器 |
* 会话管理器 |
||||
*/ |
*/ |
||||
@Bean |
@Bean |
||||
public SessionManager sessionManager() |
public SessionManager sessionManager() |
||||
{ |
{ |
||||
DefaultWebSessionManager manager = new DefaultWebSessionManager(); |
DefaultWebSessionManager manager = new DefaultWebSessionManager(); |
||||
// 加入缓存管理器
|
// 加入缓存管理器
|
||||
manager.setCacheManager(redisCacheManager()); |
manager.setCacheManager(redisCacheManager()); |
||||
// 去掉 JSESSIONID
|
// 去掉 JSESSIONID
|
||||
manager.setSessionIdUrlRewritingEnabled(false); |
manager.setSessionIdUrlRewritingEnabled(false); |
||||
// 自定义SessionDao
|
// 自定义SessionDao
|
||||
manager.setSessionDAO(redisSessionDAO()); |
manager.setSessionDAO(redisSessionDAO()); |
||||
return manager; |
return manager; |
||||
} |
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
/** |
/** |
||||
* cookie 属性设置 |
* cookie 属性设置 |
||||
*/ |
*/ |
||||
public SimpleCookie rememberMeCookie() |
public SimpleCookie rememberMeCookie() |
||||
{ |
{ |
||||
SimpleCookie cookie = new SimpleCookie("rememberMe"); |
SimpleCookie cookie = new SimpleCookie("rememberMe"); |
||||
cookie.setDomain(domain); |
cookie.setDomain(domain); |
||||
cookie.setPath(path); |
cookie.setPath(path); |
||||
cookie.setHttpOnly(httpOnly); |
cookie.setHttpOnly(httpOnly); |
||||
cookie.setMaxAge(maxAge * 24 * 60 * 60); |
cookie.setMaxAge(maxAge * 24 * 60 * 60); |
||||
return cookie; |
return cookie; |
||||
} |
} |
||||
|
|
||||
/** |
/** |
||||
* thymeleaf模板引擎和shiro框架的整合 |
* thymeleaf模板引擎和shiro框架的整合 |
||||
*/ |
*/ |
||||
@Bean |
@Bean |
||||
public ShiroDialect shiroDialect() |
public ShiroDialect shiroDialect() |
||||
{ |
{ |
||||
return new ShiroDialect(); |
return new ShiroDialect(); |
||||
} |
} |
||||
|
|
||||
|
|
||||
@Bean |
@Bean |
||||
public static DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() |
public static DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() |
||||
{ |
{ |
||||
DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator(); |
DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator(); |
||||
defaultAdvisorAutoProxyCreator.setUsePrefix(true); |
defaultAdvisorAutoProxyCreator.setUsePrefix(true); |
||||
return defaultAdvisorAutoProxyCreator; |
return defaultAdvisorAutoProxyCreator; |
||||
} |
} |
||||
/** |
/** |
||||
* Shiro过滤器配置 |
* Shiro过滤器配置 |
||||
*/ |
*/ |
||||
@Bean |
@Bean |
||||
public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) |
public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) |
||||
{ |
{ |
||||
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); |
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); |
||||
// Shiro的核心安全接口,这个属性是必须的
|
// Shiro的核心安全接口,这个属性是必须的
|
||||
shiroFilterFactoryBean.setSecurityManager(securityManager); |
shiroFilterFactoryBean.setSecurityManager(securityManager); |
||||
// 身份认证失败,则跳转到登录页面的配置
|
// 身份认证失败,则跳转到登录页面的配置
|
||||
shiroFilterFactoryBean.setLoginUrl(loginUrl); |
shiroFilterFactoryBean.setLoginUrl(loginUrl); |
||||
// 权限认证失败,则跳转到指定页面
|
// 权限认证失败,则跳转到指定页面
|
||||
shiroFilterFactoryBean.setUnauthorizedUrl(unauthorizedUrl); |
shiroFilterFactoryBean.setUnauthorizedUrl(unauthorizedUrl); |
||||
// Shiro连接约束配置,即过滤链的定义
|
// Shiro连接约束配置,即过滤链的定义
|
||||
LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<>(); |
LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<>(); |
||||
// 对静态资源设置匿名访问
|
// 对静态资源设置匿名访问
|
||||
filterChainDefinitionMap.put("/favicon.ico**", "anon"); |
filterChainDefinitionMap.put("/favicon.ico**", "anon"); |
||||
filterChainDefinitionMap.put("/ruoyi.png**", "anon"); |
filterChainDefinitionMap.put("/ruoyi.png**", "anon"); |
||||
filterChainDefinitionMap.put("/css/**", "anon"); |
filterChainDefinitionMap.put("/css/**", "anon"); |
||||
filterChainDefinitionMap.put("/docs/**", "anon"); |
filterChainDefinitionMap.put("/docs/**", "anon"); |
||||
filterChainDefinitionMap.put("/fonts/**", "anon"); |
filterChainDefinitionMap.put("/fonts/**", "anon"); |
||||
filterChainDefinitionMap.put("/img/**", "anon"); |
filterChainDefinitionMap.put("/img/**", "anon"); |
||||
filterChainDefinitionMap.put("/ajax/**", "anon"); |
filterChainDefinitionMap.put("/ajax/**", "anon"); |
||||
filterChainDefinitionMap.put("/js/**", "anon"); |
filterChainDefinitionMap.put("/js/**", "anon"); |
||||
filterChainDefinitionMap.put("/ruoyi/**", "anon"); |
filterChainDefinitionMap.put("/ruoyi/**", "anon"); |
||||
filterChainDefinitionMap.put("/hwwebscan_verify.html", "anon"); |
filterChainDefinitionMap.put("/hwwebscan_verify.html", "anon"); |
||||
|
|
||||
// 金蝶同步数据不拦截
|
// 金蝶同步数据不拦截
|
||||
filterChainDefinitionMap.put("/kingdee/dataSync/**", "anon"); |
filterChainDefinitionMap.put("/kingdee/dataSync/**", "anon"); |
||||
|
|
||||
filterChainDefinitionMap.put("/captcha/captchaImage**", "anon"); |
filterChainDefinitionMap.put("/captcha/captchaImage**", "anon"); |
||||
// 退出 logout地址,shiro去清除session
|
// 退出 logout地址,shiro去清除session
|
||||
filterChainDefinitionMap.put("/logout", "logout"); |
filterChainDefinitionMap.put("/logout", "logout"); |
||||
|
|
||||
// 不拦截app登录
|
// 不拦截app登录
|
||||
filterChainDefinitionMap.put("/appAjaxLogin", "anon"); |
filterChainDefinitionMap.put("/appAjaxLogin", "anon"); |
||||
filterChainDefinitionMap.put("/app/appLogin", "anon"); |
filterChainDefinitionMap.put("/app/appLogin", "anon"); |
||||
filterChainDefinitionMap.put("/app/**", "anon"); |
filterChainDefinitionMap.put("/app/**", "anon"); |
||||
|
filterChainDefinitionMap.put("/small/**", "anon"); |
||||
// 对外开放的controller不拦截
|
|
||||
filterChainDefinitionMap.put("/external/**", "anon"); |
// 对外开放的controller不拦截
|
||||
|
filterChainDefinitionMap.put("/external/**", "anon"); |
||||
// 附件访问不拦截
|
|
||||
filterChainDefinitionMap.put("/profile/**", "anon"); |
// 附件访问不拦截
|
||||
// 系统权限列表
|
filterChainDefinitionMap.put("/profile/**", "anon"); |
||||
// filterChainDefinitionMap.putAll(SpringUtils.getBean(IMenuService.class).selectPermsAll());
|
// 系统权限列表
|
||||
|
// filterChainDefinitionMap.putAll(SpringUtils.getBean(IMenuService.class).selectPermsAll());
|
||||
Map<String, Filter> filters = new LinkedHashMap<String, Filter>(); |
|
||||
|
Map<String, Filter> filters = new LinkedHashMap<String, Filter>(); |
||||
shiroFilterFactoryBean.setFilters(filters); |
|
||||
|
shiroFilterFactoryBean.setFilters(filters); |
||||
// 所有请求需要认证
|
|
||||
|
// 所有请求需要认证
|
||||
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); |
|
||||
|
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); |
||||
return shiroFilterFactoryBean; |
|
||||
} |
return shiroFilterFactoryBean; |
||||
} |
} |
||||
|
} |
||||
|
|||||
Loading…
Reference in new issue