system改造

This commit is contained in:
zhuyang
2021-10-13 17:07:09 +08:00
parent 341404eafa
commit db27985036
57 changed files with 855 additions and 934 deletions

View File

@@ -2,20 +2,20 @@ package cn.iocoder.mall.security.admin.core.interceptor;
import cn.iocoder.common.framework.enums.UserTypeEnum;
import cn.iocoder.common.framework.exception.GlobalException;
import cn.iocoder.common.framework.exception.util.ServiceExceptionUtil;
import cn.iocoder.common.framework.util.CollectionUtils;
import cn.iocoder.common.framework.util.HttpUtil;
import cn.iocoder.common.framework.exception.util.ServiceExceptionUtil;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.security.admin.core.context.AdminSecurityContext;
import cn.iocoder.mall.security.admin.core.context.AdminSecurityContextHolder;
import cn.iocoder.mall.systemservice.rpc.oauth.OAuth2Rpc;
import cn.iocoder.mall.systemservice.rpc.oauth.OAuthFeign;
import cn.iocoder.mall.systemservice.rpc.oauth.dto.OAuth2AccessTokenRespDTO;
import cn.iocoder.mall.systemservice.rpc.permission.PermissionRpc;
import cn.iocoder.mall.systemservice.rpc.permission.PermissionFeign;
import cn.iocoder.mall.systemservice.rpc.permission.dto.PermissionCheckDTO;
import cn.iocoder.mall.web.core.util.CommonWebUtil;
import cn.iocoder.security.annotations.RequiresNone;
import cn.iocoder.security.annotations.RequiresPermissions;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
@@ -28,11 +28,11 @@ import static cn.iocoder.mall.systemservice.enums.SystemErrorCodeConstants.OAUTH
public class AdminSecurityInterceptor extends HandlerInterceptorAdapter {
@Reference(version = "${dubbo.consumer.OAuth2Rpc.version}")
private OAuth2Rpc oauth2Rpc;
@Reference(version = "${dubbo.consumer.PermissionRpc.version}")
private PermissionRpc permissionRpc;
@Autowired
private OAuthFeign oAuthFeign;
@Autowired
private PermissionFeign permissionFeign;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
// 获得访问令牌
@@ -48,7 +48,7 @@ public class AdminSecurityInterceptor extends HandlerInterceptorAdapter {
String accessToken = HttpUtil.obtainAuthorization(request);
Integer adminId = null;
if (accessToken != null) {
CommonResult<OAuth2AccessTokenRespDTO> checkAccessTokenResult = oauth2Rpc.checkAccessToken(accessToken);
CommonResult<OAuth2AccessTokenRespDTO> checkAccessTokenResult = oAuthFeign.checkAccessToken(accessToken);
checkAccessTokenResult.checkError();
// 校验用户类型正确
if (!UserTypeEnum.ADMIN.getValue().equals(checkAccessTokenResult.getData().getUserType())) {
@@ -83,7 +83,7 @@ public class AdminSecurityInterceptor extends HandlerInterceptorAdapter {
return;
}
// 权限验证
permissionRpc.checkPermission(new PermissionCheckDTO().setAdminId(adminId).setPermissions(Arrays.asList(permissions)))
permissionFeign.checkPermission(new PermissionCheckDTO().setAdminId(adminId).setPermissions(Arrays.asList(permissions)))
.checkError();
}

View File

@@ -6,12 +6,12 @@ import cn.iocoder.common.framework.util.HttpUtil;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.security.user.core.context.UserSecurityContext;
import cn.iocoder.mall.security.user.core.context.UserSecurityContextHolder;
import cn.iocoder.mall.systemservice.rpc.oauth.OAuth2Rpc;
import cn.iocoder.mall.systemservice.rpc.oauth.OAuthFeign;
import cn.iocoder.mall.systemservice.rpc.oauth.dto.OAuth2AccessTokenRespDTO;
import cn.iocoder.mall.web.core.util.CommonWebUtil;
import cn.iocoder.security.annotations.RequiresAuthenticate;
import cn.iocoder.security.annotations.RequiresPermissions;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
@@ -23,8 +23,8 @@ import static cn.iocoder.mall.systemservice.enums.SystemErrorCodeConstants.OAUTH
public class UserSecurityInterceptor extends HandlerInterceptorAdapter {
@Reference(version = "${dubbo.consumer.OAuth2Rpc.version}")
private OAuth2Rpc oauth2Rpc;
@Autowired
private OAuthFeign oAuthFeign;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
@@ -39,7 +39,7 @@ public class UserSecurityInterceptor extends HandlerInterceptorAdapter {
String accessToken = HttpUtil.obtainAuthorization(request);
Integer userId = null;
if (accessToken != null) {
CommonResult<OAuth2AccessTokenRespDTO> checkAccessTokenResult = oauth2Rpc.checkAccessToken(accessToken);
CommonResult<OAuth2AccessTokenRespDTO> checkAccessTokenResult = oAuthFeign.checkAccessToken(accessToken);
checkAccessTokenResult.checkError();
// 校验用户类型正确
if (!UserTypeEnum.USER.getValue().equals(checkAccessTokenResult.getData().getUserType())) {

View File

@@ -3,11 +3,11 @@ package cn.iocoder.mall.system.errorcode.core;
import cn.iocoder.common.framework.exception.ErrorCode;
import cn.iocoder.common.framework.util.StringUtils;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.systemservice.rpc.errorcode.ErrorCodeRpc;
import cn.iocoder.mall.systemservice.rpc.errorcode.ErrorCodeFeign;
import cn.iocoder.mall.systemservice.rpc.errorcode.dto.ErrorCodeAutoGenerateDTO;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
@@ -29,9 +29,9 @@ public class ErrorCodeAutoGenerator {
*/
private String errorCodeConstantsClass;
@Reference(version = "${dubbo.consumer.ErrorCodeRpc.version}")
private ErrorCodeRpc errorCodeRpc;
@Autowired
private ErrorCodeFeign errorCodeFeign;
public ErrorCodeAutoGenerator(String group) {
this.group = group;
}
@@ -72,7 +72,7 @@ public class ErrorCodeAutoGenerator {
throw new RuntimeException(e);
}
});
CommonResult<Boolean> autoGenerateErrorCodesResult = errorCodeRpc.autoGenerateErrorCodes(autoGenerateDTOs);
CommonResult<Boolean> autoGenerateErrorCodesResult = errorCodeFeign.autoGenerateErrorCodes(autoGenerateDTOs);
if (autoGenerateErrorCodesResult.isSuccess()) {
logger.info("[execute][自动将 ({}) 类的错误码,成功写入到 system-service 服务]", errorCodeConstantsClass);
} else {

View File

@@ -1,14 +1,14 @@
package cn.iocoder.mall.system.errorcode.core;
import cn.iocoder.common.framework.exception.util.ServiceExceptionUtil;
import cn.iocoder.common.framework.util.CollectionUtils;
import cn.iocoder.common.framework.util.DateUtil;
import cn.iocoder.common.framework.exception.util.ServiceExceptionUtil;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.systemservice.rpc.errorcode.ErrorCodeRpc;
import cn.iocoder.mall.systemservice.rpc.errorcode.ErrorCodeFeign;
import cn.iocoder.mall.systemservice.rpc.errorcode.vo.ErrorCodeVO;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Scheduled;
@@ -27,9 +27,8 @@ public class ErrorCodeRemoteLoader {
*/
private final String group;
@Reference(version = "${dubbo.consumer.ErrorCodeRpc.version}")
private ErrorCodeRpc errorCodeRpc;
@Autowired
private ErrorCodeFeign errorCodeFeign;
private Date maxUpdateTime;
public ErrorCodeRemoteLoader(String group) {
@@ -38,8 +37,8 @@ public class ErrorCodeRemoteLoader {
@EventListener(ApplicationReadyEvent.class)
public void loadErrorCodes() {
// 从 ErrorCodeRpc 全量加载 ErrorCode 错误码
CommonResult<List<ErrorCodeVO>> listErrorCodesResult = errorCodeRpc.listErrorCodes(group, null);
// 从 errorCodeFeign 全量加载 ErrorCode 错误码
CommonResult<List<ErrorCodeVO>> listErrorCodesResult = errorCodeFeign.listErrorCodes(group, null);
listErrorCodesResult.checkError();
logger.info("[loadErrorCodes][从 group({}) 全量加载到 {} 个 ErrorCode 错误码]", group, listErrorCodesResult.getData().size());
// 写入到 ServiceExceptionUtil 到
@@ -52,9 +51,9 @@ public class ErrorCodeRemoteLoader {
@Scheduled(fixedDelay = REFRESH_ERROR_CODE_PERIOD, initialDelay = REFRESH_ERROR_CODE_PERIOD)
public void refreshErrorCodes() {
// 从 ErrorCodeRpc 增量加载 ErrorCode 错误码
// 从 errorCodeFeign 增量加载 ErrorCode 错误码
// TODO 优化点:假设删除错误码的配置,会存在问题;
CommonResult<List<ErrorCodeVO>> listErrorCodesResult = errorCodeRpc.listErrorCodes(group, maxUpdateTime);
CommonResult<List<ErrorCodeVO>> listErrorCodesResult = errorCodeFeign.listErrorCodes(group, maxUpdateTime);
listErrorCodesResult.checkError();
if (CollectionUtils.isEmpty(listErrorCodesResult.getData())) {
return;

View File

@@ -48,7 +48,7 @@ public class CommonWebAutoConfiguration implements WebMvcConfigurer {
// ========== 拦截器相关 ==========
@Bean
@ConditionalOnClass(name = {"cn.iocoder.mall.systemservice.rpc.systemlog.SystemExceptionLogRpc", "org.apache.dubbo.config.annotation.Reference"})
@ConditionalOnClass(name = {"cn.iocoder.mall.systemservice.rpc.systemlog.SystemExceptionLogFeign", "org.apache.dubbo.config.annotation.Reference"})
@ConditionalOnMissingBean(AccessLogInterceptor.class)
public AccessLogInterceptor accessLogInterceptor() {
return new AccessLogInterceptor();

View File

@@ -7,14 +7,14 @@ import cn.iocoder.common.framework.util.ExceptionUtil;
import cn.iocoder.common.framework.util.HttpUtil;
import cn.iocoder.common.framework.util.MallUtils;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.systemservice.rpc.systemlog.SystemExceptionLogRpc;
import cn.iocoder.mall.systemservice.rpc.systemlog.SystemExceptionLogFeign;
import cn.iocoder.mall.systemservice.rpc.systemlog.dto.SystemExceptionLogCreateDTO;
import cn.iocoder.mall.web.core.util.CommonWebUtil;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.util.Assert;
@@ -34,7 +34,8 @@ import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import java.util.Date;
import static cn.iocoder.common.framework.exception.enums.GlobalErrorCodeConstants.*;
import static cn.iocoder.common.framework.exception.enums.GlobalErrorCodeConstants.BAD_REQUEST;
import static cn.iocoder.common.framework.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
/**
* 全局异常处理器,将 Exception 翻译成 CommonResult + 对应的异常编号
@@ -55,9 +56,9 @@ public class GlobalExceptionHandler {
private String applicationName;
// TODO 目前存在一个问题,如果未引入 system-rpc-api 依赖GlobalExceptionHandler 会报类不存在。未来封装出 Repository 解决该问题
@Reference(version = "${dubbo.consumer.SystemExceptionLogRpc.version}")
private SystemExceptionLogRpc systemExceptionLogRpc;
@Autowired
private SystemExceptionLogFeign systemExceptionLogFeign;
/**
* 处理 SpringMVC 请求参数缺失
*
@@ -216,7 +217,7 @@ public class GlobalExceptionHandler {
@Async
public void createExceptionLog(SystemExceptionLogCreateDTO exceptionLog) {
try {
systemExceptionLogRpc.createSystemExceptionLog(exceptionLog);
systemExceptionLogFeign.createSystemExceptionLog(exceptionLog);
} catch (Throwable th) {
logger.error("[addAccessLog][插入异常日志({}) 发生异常({})", JSON.toJSONString(exceptionLog), ExceptionUtils.getRootCauseMessage(th));
}

View File

@@ -3,14 +3,14 @@ package cn.iocoder.mall.web.core.interceptor;
import cn.iocoder.common.framework.util.HttpUtil;
import cn.iocoder.common.framework.util.MallUtils;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.systemservice.rpc.systemlog.SystemAccessLogRpc;
import cn.iocoder.mall.systemservice.rpc.systemlog.SystemAccessLogFeign;
import cn.iocoder.mall.systemservice.rpc.systemlog.dto.SystemAccessLogCreateDTO;
import cn.iocoder.mall.web.core.util.CommonWebUtil;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
@@ -26,9 +26,9 @@ public class AccessLogInterceptor extends HandlerInterceptorAdapter {
private Logger logger = LoggerFactory.getLogger(getClass());
@Reference(version = "${dubbo.consumer.SystemAccessLogRpc.version}")
private SystemAccessLogRpc systemAccessLogRpc;
@Autowired
private SystemAccessLogFeign systemAccessLogFeign;
@Value("${spring.application.name}")
private String applicationName;
@@ -81,7 +81,7 @@ public class AccessLogInterceptor extends HandlerInterceptorAdapter {
@Async // 异步入库
public void addAccessLog(SystemAccessLogCreateDTO accessLog) {
try {
systemAccessLogRpc.createSystemAccessLog(accessLog);
systemAccessLogFeign.createSystemAccessLog(accessLog);
} catch (Throwable th) {
logger.error("[addAccessLog][插入访问日志({}) 发生异常({})", JSON.toJSONString(accessLog), ExceptionUtils.getRootCauseMessage(th));
}