|
@@ -1,26 +1,6 @@
|
|
|
package beilv.framework.config;
|
|
|
|
|
|
-import java.io.ByteArrayInputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.util.LinkedHashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import javax.servlet.Filter;
|
|
|
-import org.apache.commons.io.IOUtils;
|
|
|
-import org.apache.shiro.cache.ehcache.EhCacheManager;
|
|
|
-import org.apache.shiro.codec.Base64;
|
|
|
-import org.apache.shiro.config.ConfigurationException;
|
|
|
-import org.apache.shiro.io.ResourceUtils;
|
|
|
-import org.apache.shiro.mgt.SecurityManager;
|
|
|
-import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
|
|
|
-import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
|
|
|
-import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
|
|
|
-import org.apache.shiro.web.servlet.SimpleCookie;
|
|
|
-import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
-import org.springframework.context.annotation.Bean;
|
|
|
-import org.springframework.context.annotation.Configuration;
|
|
|
+import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
|
|
|
import beilv.common.constant.Constants;
|
|
|
import beilv.common.utils.StringUtils;
|
|
|
import beilv.common.utils.security.CipherUtils;
|
|
@@ -32,22 +12,46 @@ import beilv.framework.shiro.session.OnlineSessionDAO;
|
|
|
import beilv.framework.shiro.session.OnlineSessionFactory;
|
|
|
import beilv.framework.shiro.web.CustomShiroFilterFactoryBean;
|
|
|
import beilv.framework.shiro.web.filter.LogoutFilter;
|
|
|
+import beilv.framework.shiro.web.filter.wxMiniAppAccessControlFilter.WxMiniAppAccessControlFilter;
|
|
|
import beilv.framework.shiro.web.filter.captcha.CaptchaValidateFilter;
|
|
|
import beilv.framework.shiro.web.filter.kickout.KickoutSessionFilter;
|
|
|
import beilv.framework.shiro.web.filter.online.OnlineSessionFilter;
|
|
|
import beilv.framework.shiro.web.filter.sync.SyncOnlineSessionFilter;
|
|
|
import beilv.framework.shiro.web.session.OnlineWebSessionManager;
|
|
|
import beilv.framework.shiro.web.session.SpringSessionValidationScheduler;
|
|
|
-import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
|
+import org.apache.shiro.cache.ehcache.EhCacheManager;
|
|
|
+import org.apache.shiro.codec.Base64;
|
|
|
+import org.apache.shiro.config.ConfigurationException;
|
|
|
+import org.apache.shiro.io.ResourceUtils;
|
|
|
+import org.apache.shiro.mgt.SecurityManager;
|
|
|
+import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
|
|
|
+import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
|
|
|
+import org.apache.shiro.web.filter.mgt.DefaultFilterChainManager;
|
|
|
+import org.apache.shiro.web.filter.mgt.FilterChainManager;
|
|
|
+import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
|
|
|
+import org.apache.shiro.web.servlet.SimpleCookie;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+
|
|
|
+import javax.servlet.Filter;
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* 权限配置加载
|
|
|
- *
|
|
|
+ *
|
|
|
* @author ruoyi
|
|
|
*/
|
|
|
@Configuration
|
|
|
-public class ShiroConfig
|
|
|
-{
|
|
|
+public class ShiroConfig {
|
|
|
/**
|
|
|
* Session超时时间,单位为毫秒(默认30分钟)
|
|
|
*/
|
|
@@ -136,17 +140,13 @@ public class ShiroConfig
|
|
|
* 缓存管理器 使用Ehcache实现
|
|
|
*/
|
|
|
@Bean
|
|
|
- public EhCacheManager getEhCacheManager()
|
|
|
- {
|
|
|
+ public EhCacheManager getEhCacheManager() {
|
|
|
net.sf.ehcache.CacheManager cacheManager = net.sf.ehcache.CacheManager.getCacheManager("ruoyi");
|
|
|
EhCacheManager em = new EhCacheManager();
|
|
|
- if (StringUtils.isNull(cacheManager))
|
|
|
- {
|
|
|
+ if (StringUtils.isNull(cacheManager)) {
|
|
|
em.setCacheManager(new net.sf.ehcache.CacheManager(getCacheManagerConfigFileInputStream()));
|
|
|
return em;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
+ } else {
|
|
|
em.setCacheManager(cacheManager);
|
|
|
return em;
|
|
|
}
|
|
@@ -155,24 +155,18 @@ public class ShiroConfig
|
|
|
/**
|
|
|
* 返回配置文件流 避免ehcache配置文件一直被占用,无法完全销毁项目重新部署
|
|
|
*/
|
|
|
- protected InputStream getCacheManagerConfigFileInputStream()
|
|
|
- {
|
|
|
+ protected InputStream getCacheManagerConfigFileInputStream() {
|
|
|
String configFile = "classpath:ehcache/ehcache-shiro.xml";
|
|
|
InputStream inputStream = null;
|
|
|
- try
|
|
|
- {
|
|
|
+ try {
|
|
|
inputStream = ResourceUtils.getInputStreamForPath(configFile);
|
|
|
byte[] b = IOUtils.toByteArray(inputStream);
|
|
|
InputStream in = new ByteArrayInputStream(b);
|
|
|
return in;
|
|
|
- }
|
|
|
- catch (IOException e)
|
|
|
- {
|
|
|
+ } catch (IOException e) {
|
|
|
throw new ConfigurationException(
|
|
|
"Unable to obtain input stream for cacheManagerConfigFile [" + configFile + "]", e);
|
|
|
- }
|
|
|
- finally
|
|
|
- {
|
|
|
+ } finally {
|
|
|
IOUtils.closeQuietly(inputStream);
|
|
|
}
|
|
|
}
|
|
@@ -181,8 +175,7 @@ public class ShiroConfig
|
|
|
* 自定义Realm
|
|
|
*/
|
|
|
@Bean
|
|
|
- public UserRealm userRealm(EhCacheManager cacheManager)
|
|
|
- {
|
|
|
+ public UserRealm userRealm(EhCacheManager cacheManager) {
|
|
|
UserRealm userRealm = new UserRealm();
|
|
|
userRealm.setAuthorizationCacheName(Constants.SYS_AUTH_CACHE);
|
|
|
userRealm.setCacheManager(cacheManager);
|
|
@@ -193,8 +186,7 @@ public class ShiroConfig
|
|
|
* 自定义sessionDAO会话
|
|
|
*/
|
|
|
@Bean
|
|
|
- public OnlineSessionDAO sessionDAO()
|
|
|
- {
|
|
|
+ public OnlineSessionDAO sessionDAO() {
|
|
|
OnlineSessionDAO sessionDAO = new OnlineSessionDAO();
|
|
|
return sessionDAO;
|
|
|
}
|
|
@@ -203,8 +195,7 @@ public class ShiroConfig
|
|
|
* 自定义sessionFactory会话
|
|
|
*/
|
|
|
@Bean
|
|
|
- public OnlineSessionFactory sessionFactory()
|
|
|
- {
|
|
|
+ public OnlineSessionFactory sessionFactory() {
|
|
|
OnlineSessionFactory sessionFactory = new OnlineSessionFactory();
|
|
|
return sessionFactory;
|
|
|
}
|
|
@@ -213,8 +204,7 @@ public class ShiroConfig
|
|
|
* 会话管理器
|
|
|
*/
|
|
|
@Bean
|
|
|
- public OnlineWebSessionManager sessionManager()
|
|
|
- {
|
|
|
+ public OnlineWebSessionManager sessionManager() {
|
|
|
OnlineWebSessionManager manager = new OnlineWebSessionManager();
|
|
|
// 加入缓存管理器
|
|
|
manager.setCacheManager(getEhCacheManager());
|
|
@@ -239,8 +229,7 @@ public class ShiroConfig
|
|
|
* 安全管理器
|
|
|
*/
|
|
|
@Bean
|
|
|
- public SecurityManager securityManager(UserRealm userRealm)
|
|
|
- {
|
|
|
+ public SecurityManager securityManager(UserRealm userRealm) {
|
|
|
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
|
|
|
// 设置realm.
|
|
|
securityManager.setRealm(userRealm);
|
|
@@ -256,19 +245,18 @@ public class ShiroConfig
|
|
|
/**
|
|
|
* 退出过滤器
|
|
|
*/
|
|
|
- public LogoutFilter logoutFilter()
|
|
|
- {
|
|
|
+ public LogoutFilter logoutFilter() {
|
|
|
LogoutFilter logoutFilter = new LogoutFilter();
|
|
|
logoutFilter.setLoginUrl(loginUrl);
|
|
|
return logoutFilter;
|
|
|
}
|
|
|
-
|
|
|
+ @Autowired
|
|
|
+ private WxMiniAppAccessControlFilter wxMiniAppAccessControlFilter;
|
|
|
/**
|
|
|
* Shiro过滤器配置
|
|
|
*/
|
|
|
@Bean
|
|
|
- public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager)
|
|
|
- {
|
|
|
+ public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
|
|
|
CustomShiroFilterFactoryBean shiroFilterFactoryBean = new CustomShiroFilterFactoryBean();
|
|
|
// Shiro的核心安全接口,这个属性是必须的
|
|
|
shiroFilterFactoryBean.setSecurityManager(securityManager);
|
|
@@ -292,14 +280,15 @@ public class ShiroConfig
|
|
|
filterChainDefinitionMap.put("/captcha/captchaImage**", "anon");
|
|
|
// 匿名访问不鉴权注解列表
|
|
|
List<String> permitAllUrl = SpringUtils.getBean(PermitAllUrlProperties.class).getUrls();
|
|
|
- if (StringUtils.isNotEmpty(permitAllUrl))
|
|
|
- {
|
|
|
+ if (StringUtils.isNotEmpty(permitAllUrl)) {
|
|
|
permitAllUrl.forEach(url -> filterChainDefinitionMap.put(url, "anon"));
|
|
|
}
|
|
|
// 退出 logout地址,shiro去清除session
|
|
|
filterChainDefinitionMap.put("/logout", "logout");
|
|
|
// 不需要拦截的访问
|
|
|
filterChainDefinitionMap.put("/login", "anon,captchaValidate");
|
|
|
+ // 微信小程序登录
|
|
|
+// filterChainDefinitionMap.put("/app-api/**", "anon,captchaValidate");
|
|
|
// 注册相关
|
|
|
filterChainDefinitionMap.put("/register", "anon,captchaValidate");
|
|
|
// 系统权限列表
|
|
@@ -310,11 +299,13 @@ public class ShiroConfig
|
|
|
filters.put("syncOnlineSession", syncOnlineSessionFilter());
|
|
|
filters.put("captchaValidate", captchaValidateFilter());
|
|
|
filters.put("kickout", kickoutSessionFilter());
|
|
|
+ filters.put("wxAccessControl", wxMiniAppAccessControlFilter); // 添加自定义过滤器
|
|
|
// 注销成功,则跳转到指定页面
|
|
|
filters.put("logout", logoutFilter());
|
|
|
shiroFilterFactoryBean.setFilters(filters);
|
|
|
|
|
|
// 所有请求需要认证
|
|
|
+ filterChainDefinitionMap.put("/app-api/**", "wxAccessControl");
|
|
|
filterChainDefinitionMap.put("/**", "user,kickout,onlineSession,syncOnlineSession");
|
|
|
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
|
|
|
|
|
@@ -324,8 +315,7 @@ public class ShiroConfig
|
|
|
/**
|
|
|
* 自定义在线用户处理过滤器
|
|
|
*/
|
|
|
- public OnlineSessionFilter onlineSessionFilter()
|
|
|
- {
|
|
|
+ public OnlineSessionFilter onlineSessionFilter() {
|
|
|
OnlineSessionFilter onlineSessionFilter = new OnlineSessionFilter();
|
|
|
onlineSessionFilter.setLoginUrl(loginUrl);
|
|
|
onlineSessionFilter.setOnlineSessionDAO(sessionDAO());
|
|
@@ -335,8 +325,7 @@ public class ShiroConfig
|
|
|
/**
|
|
|
* 自定义在线用户同步过滤器
|
|
|
*/
|
|
|
- public SyncOnlineSessionFilter syncOnlineSessionFilter()
|
|
|
- {
|
|
|
+ public SyncOnlineSessionFilter syncOnlineSessionFilter() {
|
|
|
SyncOnlineSessionFilter syncOnlineSessionFilter = new SyncOnlineSessionFilter();
|
|
|
syncOnlineSessionFilter.setOnlineSessionDAO(sessionDAO());
|
|
|
return syncOnlineSessionFilter;
|
|
@@ -345,8 +334,7 @@ public class ShiroConfig
|
|
|
/**
|
|
|
* 自定义验证码过滤器
|
|
|
*/
|
|
|
- public CaptchaValidateFilter captchaValidateFilter()
|
|
|
- {
|
|
|
+ public CaptchaValidateFilter captchaValidateFilter() {
|
|
|
CaptchaValidateFilter captchaValidateFilter = new CaptchaValidateFilter();
|
|
|
captchaValidateFilter.setCaptchaEnabled(captchaEnabled);
|
|
|
captchaValidateFilter.setCaptchaType(captchaType);
|
|
@@ -356,8 +344,7 @@ public class ShiroConfig
|
|
|
/**
|
|
|
* cookie 属性设置
|
|
|
*/
|
|
|
- public SimpleCookie rememberMeCookie()
|
|
|
- {
|
|
|
+ public SimpleCookie rememberMeCookie() {
|
|
|
SimpleCookie cookie = new SimpleCookie("rememberMe");
|
|
|
cookie.setDomain(domain);
|
|
|
cookie.setPath(path);
|
|
@@ -369,16 +356,12 @@ public class ShiroConfig
|
|
|
/**
|
|
|
* 记住我
|
|
|
*/
|
|
|
- public CustomCookieRememberMeManager rememberMeManager()
|
|
|
- {
|
|
|
+ public CustomCookieRememberMeManager rememberMeManager() {
|
|
|
CustomCookieRememberMeManager cookieRememberMeManager = new CustomCookieRememberMeManager();
|
|
|
cookieRememberMeManager.setCookie(rememberMeCookie());
|
|
|
- if (StringUtils.isNotEmpty(cipherKey))
|
|
|
- {
|
|
|
+ if (StringUtils.isNotEmpty(cipherKey)) {
|
|
|
cookieRememberMeManager.setCipherKey(Base64.decode(cipherKey));
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
+ } else {
|
|
|
cookieRememberMeManager.setCipherKey(CipherUtils.generateNewKey(128, "AES").getEncoded());
|
|
|
}
|
|
|
return cookieRememberMeManager;
|
|
@@ -387,8 +370,7 @@ public class ShiroConfig
|
|
|
/**
|
|
|
* 同一个用户多设备登录限制
|
|
|
*/
|
|
|
- public KickoutSessionFilter kickoutSessionFilter()
|
|
|
- {
|
|
|
+ public KickoutSessionFilter kickoutSessionFilter() {
|
|
|
KickoutSessionFilter kickoutSessionFilter = new KickoutSessionFilter();
|
|
|
kickoutSessionFilter.setCacheManager(getEhCacheManager());
|
|
|
kickoutSessionFilter.setSessionManager(sessionManager());
|
|
@@ -405,8 +387,7 @@ public class ShiroConfig
|
|
|
* thymeleaf模板引擎和shiro框架的整合
|
|
|
*/
|
|
|
@Bean
|
|
|
- public ShiroDialect shiroDialect()
|
|
|
- {
|
|
|
+ public ShiroDialect shiroDialect() {
|
|
|
return new ShiroDialect();
|
|
|
}
|
|
|
|
|
@@ -415,10 +396,10 @@ public class ShiroConfig
|
|
|
*/
|
|
|
@Bean
|
|
|
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(
|
|
|
- @Qualifier("securityManager") SecurityManager securityManager)
|
|
|
- {
|
|
|
+ @Qualifier("securityManager") SecurityManager securityManager) {
|
|
|
AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
|
|
|
authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
|
|
|
return authorizationAttributeSourceAdvisor;
|
|
|
}
|
|
|
+
|
|
|
}
|