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