新增组织失效/过期校验:1、登录校验;2、已登录用户checkToken和机构校验过滤器校验;

This commit is contained in:
gaoqr
2024-08-02 11:24:03 +08:00
parent 355332b23d
commit a6ce630a8c
4 changed files with 39 additions and 49 deletions
@@ -1,6 +1,7 @@
package com.cf.imes.framework.organ.core.security;
import cn.hutool.core.collection.CollUtil;
import com.cf.imes.framework.common.exception.ServiceException;
import com.cf.imes.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.util.servlet.ServletUtils;
@@ -13,6 +14,7 @@ import com.cf.imes.framework.web.config.WebProperties;
import com.cf.imes.framework.web.core.filter.ApiRequestFilter;
import com.cf.imes.framework.web.core.handler.GlobalExceptionHandler;
import com.cf.imes.framework.web.core.util.WebFrameworkUtils;
import com.cf.imes.module.system.enums.ErrorCodeConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;
@@ -90,6 +92,10 @@ public class OrganSecurityWebFilter extends ApiRequestFilter {
organFrameworkService.validOrgan(organId);
} catch (Throwable ex) {
CommonResult<?> result = globalExceptionHandler.allExceptionHandler(request, ex);
// 组织失效返回401踢出系统
if (needUnauthorizedWhenOrgExpired(ex)) {
result.setCode(GlobalErrorCodeConstants.UNAUTHORIZED.getCode());
}
ServletUtils.writeJSON(response, result);
return;
}
@@ -117,4 +123,21 @@ public class OrganSecurityWebFilter extends ApiRequestFilter {
return false;
}
/**
* 是否组织失效设置未登录回调
*
* @param ex
* @return
*/
private boolean needUnauthorizedWhenOrgExpired(Throwable ex) {
if (ex instanceof ServiceException) {
ServiceException serviceException = (ServiceException) ex;
if (Objects.equals(serviceException.getCode(), ErrorCodeConstants.ORGAN_EXPIRE.getCode())
|| Objects.equals(serviceException.getCode(), ErrorCodeConstants.ORGAN_DISABLE.getCode())
|| Objects.equals(serviceException.getCode(), ErrorCodeConstants.ORGAN_NOT_EXISTS.getCode())) {
return true;
}
}
return false;
}
}
@@ -1,6 +1,5 @@
package com.cf.imes.framework.organ.core.service;
import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.util.cache.CacheUtils;
import com.cf.imes.module.system.api.organ.OrganApi;
import com.google.common.cache.CacheLoader;
@@ -26,7 +25,7 @@ public class OrganFrameworkServiceImpl implements OrganFrameworkService {
*/
private final LoadingCache<Object, List<Long>> getOrganIdsCache = CacheUtils.buildAsyncReloadingCache(
Duration.ofMinutes(1L), // 过期时间 1 分钟
new CacheLoader<Object, List<Long>>() {
new CacheLoader<>() {
@Override
public List<Long> load(Object key) {
@@ -35,20 +34,6 @@ public class OrganFrameworkServiceImpl implements OrganFrameworkService {
});
/**
* 针对 {@link #validOrgan(Long)} 的缓存
*/
private final LoadingCache<Long, CommonResult<Boolean>> validOrganCache = CacheUtils.buildAsyncReloadingCache(
Duration.ofMinutes(1L), // 过期时间 1 分钟
new CacheLoader<Long, CommonResult<Boolean>>() {
@Override
public CommonResult<Boolean> load(Long id) {
return organApi.validOrgan(id);
}
});
@Override
@SneakyThrows
public List<Long> getOrganIds() {
@@ -58,7 +43,7 @@ public class OrganFrameworkServiceImpl implements OrganFrameworkService {
@Override
@SneakyThrows
public void validOrgan(Long id) {
validOrganCache.get(id).checkError();
organApi.validOrgan(id).checkError();
}
}