feat: 优化 授权、安装插件、更新lic、lic到期 不需要重新登录(需要刷新浏览器)才能刷新权限数据

This commit is contained in:
fit2cloud-chenyw 2021-06-07 16:11:06 +08:00
parent f1c552ff44
commit f297fd7aa2
9 changed files with 201 additions and 4 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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();
}
}

View File

@ -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);
}
}

View File

@ -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);
}
});
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -69,5 +69,18 @@
memoryStoreEvictionPolicy="LRU"
/>
<cache
name="lic_info"
eternal="false"
maxElementsInMemory="10"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="60"
timeToLiveSeconds="60"
memoryStoreEvictionPolicy="FIFO"
>
<cacheEventListenerFactory class="io.dataease.listener.LicCacheEventListener" />
</cache>
</ehcache>

View File

@ -83,6 +83,10 @@ const checkAuth = response => {
const linkToken = response.headers[LinkTokenKey.toLocaleLowerCase()]
setLinkToken(linkToken)
}
// 许可状态改变 刷新页面
// if (response.headers['lic-status']) {
// location.reload()
// }
}
// 请根据实际需求修改