diff --git a/backend/src/main/java/io/dataease/auth/config/F2CRealm.java b/backend/src/main/java/io/dataease/auth/config/F2CRealm.java index c91dbe1ff3..29c269f10f 100644 --- a/backend/src/main/java/io/dataease/auth/config/F2CRealm.java +++ b/backend/src/main/java/io/dataease/auth/config/F2CRealm.java @@ -8,6 +8,8 @@ import io.dataease.auth.entity.TokenInfo; import io.dataease.auth.service.AuthUserService; import io.dataease.auth.util.JWTUtils; import io.dataease.commons.utils.BeanUtils; +import io.dataease.commons.utils.LogUtil; +import io.dataease.listener.util.CacheUtils; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationInfo; import org.apache.shiro.authc.AuthenticationToken; @@ -54,6 +56,11 @@ public class F2CRealm extends AuthorizingRealm { @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken auth) throws AuthenticationException { + try { + CacheUtils.get("lic_info", "lic"); + }catch (Exception e) { + LogUtil.error(e); + } String token = (String) auth.getCredentials(); // 解密获得username,用于和数据库进行对比 TokenInfo tokenInfo = JWTUtils.tokenInfoByToken(token); diff --git a/backend/src/main/java/io/dataease/auth/util/JWTUtils.java b/backend/src/main/java/io/dataease/auth/util/JWTUtils.java index 8b3b7e4c92..1fa0a37f8f 100644 --- a/backend/src/main/java/io/dataease/auth/util/JWTUtils.java +++ b/backend/src/main/java/io/dataease/auth/util/JWTUtils.java @@ -6,12 +6,9 @@ import com.auth0.jwt.algorithms.Algorithm; import com.auth0.jwt.exceptions.JWTDecodeException; import com.auth0.jwt.interfaces.DecodedJWT; import io.dataease.auth.entity.TokenInfo; -import io.dataease.auth.filter.JWTFilter; import io.dataease.commons.utils.CommonBeanFactory; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; -import org.apache.shiro.authc.AuthenticationException; - import org.springframework.core.env.Environment; import java.util.Date; diff --git a/backend/src/main/java/io/dataease/listener/LicCacheEventListener.java b/backend/src/main/java/io/dataease/listener/LicCacheEventListener.java new file mode 100644 index 0000000000..632916a56f --- /dev/null +++ b/backend/src/main/java/io/dataease/listener/LicCacheEventListener.java @@ -0,0 +1,80 @@ +package io.dataease.listener; + +import io.dataease.commons.constants.AuthConstants; +import io.dataease.listener.util.CacheUtils; +import net.sf.ehcache.CacheException; +import net.sf.ehcache.Ehcache; +import net.sf.ehcache.Element; +import net.sf.ehcache.event.CacheEventListener; +import net.sf.ehcache.event.CacheEventListenerFactory; +import org.springframework.stereotype.Component; +import java.util.Properties; + +@Component +public class LicCacheEventListener extends CacheEventListenerFactory implements CacheEventListener { + + private static CacheEventListener cacheEventListener; + + public LicCacheEventListener() { + cacheEventListener = cacheEventListener == null ? this : cacheEventListener; + } + + @Override + public void notifyElementRemoved(Ehcache ehcache, Element element) throws CacheException { + /*System.out.println("notifyElementRemoved");*/ + } + + @Override + public void notifyElementPut(Ehcache ehcache, Element element) throws CacheException { + + /*long expirationTime = element.getExpirationTime(); + System.out.println(expirationTime); + System.out.println("notifyElementPut");*/ + } + + @Override + public void notifyElementUpdated(Ehcache ehcache, Element element) throws CacheException { + /*System.out.println("notifyElementUpdated");*/ + } + + /** + * lic过期触发: 清除用户、角色、权限缓存 + * @param ehcache + * @param element + */ + @Override + public void notifyElementExpired(Ehcache ehcache, Element element) { + // System.out.println("notifyElementExpired"); + /*String token = ServletUtils.getToken(); + Long userId = JWTUtils.tokenInfoByToken(token).getUserId(); + authUserService.clearCache(userId);*/ + CacheUtils.removeAll(AuthConstants.USER_CACHE_NAME); + CacheUtils.removeAll(AuthConstants.USER_ROLE_CACHE_NAME); + CacheUtils.removeAll(AuthConstants.USER_PERMISSION_CACHE_NAME); + } + + @Override + public void notifyElementEvicted(Ehcache ehcache, Element element) { + /*System.out.println("notifyElementEvicted");*/ + } + + @Override + public void notifyRemoveAll(Ehcache ehcache) { + /*System.out.println("notifyRemoveAll");*/ + } + + @Override + public void dispose() { + + } + + @Override + public CacheEventListener createCacheEventListener(Properties properties) { + return cacheEventListener; + } + + @Override + public Object clone() throws CloneNotSupportedException { + return super.clone(); + } +} diff --git a/backend/src/main/java/io/dataease/listener/util/CacheUtils.java b/backend/src/main/java/io/dataease/listener/util/CacheUtils.java new file mode 100644 index 0000000000..09e656315a --- /dev/null +++ b/backend/src/main/java/io/dataease/listener/util/CacheUtils.java @@ -0,0 +1,61 @@ +package io.dataease.listener.util; + +import net.sf.ehcache.Cache; +import net.sf.ehcache.Element; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.CacheManager; +import org.springframework.cache.ehcache.EhCacheCacheManager; +import org.springframework.context.annotation.Configuration; +import java.util.Date; + +@Configuration +public class CacheUtils { + + private static CacheManager manager; + + @Autowired + public void setManager(CacheManager manager) { + CacheUtils.manager = manager; + } + + public static Object get(String cacheName, Object key) { + Element element = cache(cacheName).get(key); + if (null == element) return null; + return element.getObjectValue(); + } + + private static void put(String cacheName, Object key, Object value, Integer ttl, Integer tti) { + Element e = new Element(key, value); + //不设置则使用xml配置 + if (ttl != null) + e.setEternal(false); + e.setTimeToLive(ttl); + if (tti != null) + e.setTimeToIdle(tti); + cache(cacheName).put(e); + } + + private static boolean remove(String cacheName, Object key) { + return cache(cacheName).remove(key); + } + + public static void removeAll(String cacheName) { + cache(cacheName).removeAll(); + } + + private static Cache cache(String cacheName) { + net.sf.ehcache.CacheManager cacheManager = ((EhCacheCacheManager) manager).getCacheManager(); + if (!cacheManager.cacheExists(cacheName)) + cacheManager.addCache(cacheName); + Cache cacheManagerCache = cacheManager.getCache(cacheName); + return cacheManagerCache; + } + + public static void updateLicCache(Date expDate){ + long time = expDate.getTime(); + long exp = (time - System.currentTimeMillis()) / 1000; + int intExp = (int)exp; + removeAll("lic_info"); + put("lic_info", "lic", "lic", intExp, intExp); + } +} diff --git a/backend/src/main/java/io/dataease/plugins/server/XAuthServer.java b/backend/src/main/java/io/dataease/plugins/server/XAuthServer.java index 3c7d0665d4..9121d531d1 100644 --- a/backend/src/main/java/io/dataease/plugins/server/XAuthServer.java +++ b/backend/src/main/java/io/dataease/plugins/server/XAuthServer.java @@ -2,18 +2,22 @@ package io.dataease.plugins.server; import io.dataease.auth.api.dto.CurrentUserDto; +import io.dataease.commons.constants.AuthConstants; import io.dataease.commons.utils.AuthUtils; import io.dataease.controller.handler.annotation.I18n; +import io.dataease.listener.util.CacheUtils; import io.dataease.plugins.config.SpringContextUtil; import io.dataease.plugins.xpack.auth.dto.request.XpackBaseTreeRequest; import io.dataease.plugins.xpack.auth.dto.request.XpackSysAuthRequest; import io.dataease.plugins.xpack.auth.dto.response.XpackSysAuthDetail; import io.dataease.plugins.xpack.auth.dto.response.XpackSysAuthDetailDTO; import io.dataease.plugins.xpack.auth.dto.response.XpackVAuthModelDTO; +import org.apache.commons.lang3.StringUtils; import org.springframework.web.bind.annotation.*; import io.dataease.plugins.xpack.auth.service.AuthXpackService; import java.util.List; import java.util.Map; +import java.util.Optional; @RequestMapping("/plugin/auth") @RestController @@ -45,5 +49,13 @@ public class XAuthServer { AuthXpackService sysAuthService = SpringContextUtil.getBean(AuthXpackService.class); CurrentUserDto user = AuthUtils.getUser(); sysAuthService.authChange(request, user.getUserId(), user.getUsername(), user.getIsAdmin()); + // 当权限发生变化 前端实时刷新对应菜单 + Optional.ofNullable(request.getAuthSourceType()).ifPresent(type -> { + if (StringUtils.equals("menu", type)) { + CacheUtils.removeAll(AuthConstants.USER_CACHE_NAME); + CacheUtils.removeAll(AuthConstants.USER_ROLE_CACHE_NAME); + CacheUtils.removeAll(AuthConstants.USER_PERMISSION_CACHE_NAME); + } + }); } } diff --git a/backend/src/main/java/io/dataease/service/AboutService.java b/backend/src/main/java/io/dataease/service/AboutService.java index e144a975e9..9568c4f6b9 100644 --- a/backend/src/main/java/io/dataease/service/AboutService.java +++ b/backend/src/main/java/io/dataease/service/AboutService.java @@ -1,16 +1,18 @@ package io.dataease.service; +import io.dataease.commons.constants.AuthConstants; import io.dataease.commons.license.DefaultLicenseService; import io.dataease.commons.license.F2CLicenseResponse; import io.dataease.commons.utils.CommonBeanFactory; import io.dataease.commons.utils.LogUtil; +import io.dataease.listener.util.CacheUtils; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.core.env.Environment; import org.springframework.stereotype.Service; - import javax.annotation.Resource; import java.io.File; +import java.util.Date; import java.util.Optional; @Service @@ -23,6 +25,15 @@ public class AboutService { public F2CLicenseResponse updateLicense(String licenseKey) { F2CLicenseResponse f2CLicenseResponse = defaultLicenseService.updateLicense(product, licenseKey); + Optional.ofNullable(f2CLicenseResponse).ifPresent(resp -> { + if (resp.getStatus() == F2CLicenseResponse.Status.valid){ + CacheUtils.updateLicCache(new Date(f2CLicenseResponse.getLicense().getExpired())); + + CacheUtils.removeAll(AuthConstants.USER_CACHE_NAME); + CacheUtils.removeAll(AuthConstants.USER_ROLE_CACHE_NAME); + CacheUtils.removeAll(AuthConstants.USER_PERMISSION_CACHE_NAME); + } + }); return f2CLicenseResponse; } diff --git a/backend/src/main/java/io/dataease/service/sys/PluginService.java b/backend/src/main/java/io/dataease/service/sys/PluginService.java index b911103bcf..880a5df0c9 100644 --- a/backend/src/main/java/io/dataease/service/sys/PluginService.java +++ b/backend/src/main/java/io/dataease/service/sys/PluginService.java @@ -5,9 +5,11 @@ import io.dataease.base.domain.MyPlugin; import io.dataease.base.mapper.MyPluginMapper; import io.dataease.base.mapper.ext.ExtSysPluginMapper; import io.dataease.base.mapper.ext.query.GridExample; +import io.dataease.commons.constants.AuthConstants; import io.dataease.commons.utils.DeFileUtils; import io.dataease.commons.utils.ZipUtils; import io.dataease.controller.sys.base.BaseGridRequest; +import io.dataease.listener.util.CacheUtils; import io.dataease.plugins.config.LoadjarUtil; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; @@ -89,6 +91,10 @@ public class PluginService { jarPath = DeFileUtils.copy(jarFile, targetDir); loadJar(jarPath, myPlugin); myPluginMapper.insert(myPlugin); + + CacheUtils.removeAll(AuthConstants.USER_CACHE_NAME); + CacheUtils.removeAll(AuthConstants.USER_ROLE_CACHE_NAME); + CacheUtils.removeAll(AuthConstants.USER_PERMISSION_CACHE_NAME); } catch (Exception e) { if (StringUtils.isNotEmpty(targetDir)) { DeFileUtils.deleteFile(targetDir); @@ -137,6 +143,9 @@ public class PluginService { * @return */ public Boolean uninstall(Long pluginId) { + CacheUtils.removeAll(AuthConstants.USER_CACHE_NAME); + CacheUtils.removeAll(AuthConstants.USER_ROLE_CACHE_NAME); + CacheUtils.removeAll(AuthConstants.USER_PERMISSION_CACHE_NAME); myPluginMapper.deleteByPrimaryKey(pluginId); return true; } @@ -148,6 +157,9 @@ public class PluginService { * @return */ public Boolean changeStatus(Long pluginId, Boolean status) { + CacheUtils.removeAll(AuthConstants.USER_CACHE_NAME); + CacheUtils.removeAll(AuthConstants.USER_ROLE_CACHE_NAME); + CacheUtils.removeAll(AuthConstants.USER_PERMISSION_CACHE_NAME); return false; } diff --git a/backend/src/main/resources/ehcache/ehcache.xml b/backend/src/main/resources/ehcache/ehcache.xml index 9491e0fa0d..a85f1cbf10 100644 --- a/backend/src/main/resources/ehcache/ehcache.xml +++ b/backend/src/main/resources/ehcache/ehcache.xml @@ -69,5 +69,18 @@ memoryStoreEvictionPolicy="LRU" /> + + + + \ No newline at end of file diff --git a/frontend/src/utils/request.js b/frontend/src/utils/request.js index e3e39b01a8..f7d7a837fc 100644 --- a/frontend/src/utils/request.js +++ b/frontend/src/utils/request.js @@ -83,6 +83,10 @@ const checkAuth = response => { const linkToken = response.headers[LinkTokenKey.toLocaleLowerCase()] setLinkToken(linkToken) } + // 许可状态改变 刷新页面 +// if (response.headers['lic-status']) { +// location.reload() +// } } // 请根据实际需求修改