From 0966f4c4ef98038cce60fc0c669e57a3ad2115bb Mon Sep 17 00:00:00 2001 From: ajaxfan <909938737@qq.com> Date: Wed, 21 Apr 2021 09:44:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0token=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E5=BC=82=E5=B8=B8=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fallback/DocumentDataServiceFallback.java | 22 +++++++++++++++ .../sdk/service/DocumentDataService.java | 22 +++++++++++++++ .../attachment-sdk-cofiguration.properties | 1 + .../impl/BaseCacheUserServiceImpl.java | 21 +++++++-------- .../entity/InvalidTokenException.java | 27 +++++++++++++++++++ ...BusinessExceptionHandlerAdviceDefault.java | 24 +++++++++++++++++ .../BusinessExceptionHandlerAdvicePro.java | 19 +++++++++++++ 7 files changed, 124 insertions(+), 12 deletions(-) create mode 100644 mall-ebtp-cloud-attachment-sdk/src/main/java/com/chinaunicom/ebtp/mall/cloud/attachment/sdk/fallback/DocumentDataServiceFallback.java create mode 100644 mall-ebtp-cloud-attachment-sdk/src/main/java/com/chinaunicom/ebtp/mall/cloud/attachment/sdk/service/DocumentDataService.java create mode 100644 uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/exception/entity/InvalidTokenException.java diff --git a/mall-ebtp-cloud-attachment-sdk/src/main/java/com/chinaunicom/ebtp/mall/cloud/attachment/sdk/fallback/DocumentDataServiceFallback.java b/mall-ebtp-cloud-attachment-sdk/src/main/java/com/chinaunicom/ebtp/mall/cloud/attachment/sdk/fallback/DocumentDataServiceFallback.java new file mode 100644 index 0000000..e53bd28 --- /dev/null +++ b/mall-ebtp-cloud-attachment-sdk/src/main/java/com/chinaunicom/ebtp/mall/cloud/attachment/sdk/fallback/DocumentDataServiceFallback.java @@ -0,0 +1,22 @@ +package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.fallback; + +import org.springframework.stereotype.Component; + +import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.service.DocumentDataService; + +import feign.Response; + +@Component +public class DocumentDataServiceFallback implements DocumentDataService { + + @Override + public Response get(String fileId) { + return null; + } + +// @Override +// public Response get() { +// return null; +// } + +} diff --git a/mall-ebtp-cloud-attachment-sdk/src/main/java/com/chinaunicom/ebtp/mall/cloud/attachment/sdk/service/DocumentDataService.java b/mall-ebtp-cloud-attachment-sdk/src/main/java/com/chinaunicom/ebtp/mall/cloud/attachment/sdk/service/DocumentDataService.java new file mode 100644 index 0000000..9a424d6 --- /dev/null +++ b/mall-ebtp-cloud-attachment-sdk/src/main/java/com/chinaunicom/ebtp/mall/cloud/attachment/sdk/service/DocumentDataService.java @@ -0,0 +1,22 @@ +package com.chinaunicom.ebtp.mall.cloud.attachment.sdk.service; + +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; + +import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.fallback.DocumentDataServiceFallback; + +import feign.Response; + +/** + * 文档中心数据服务客户端 + * + * @author Ajaxfan + */ +@FeignClient(value = "${document.data.service.id}", fallback = DocumentDataServiceFallback.class) +public interface DocumentDataService { + + @GetMapping("v1.0/files/downloadResponse") + Response get(@RequestParam("fileId") String fileId); + +} diff --git a/mall-ebtp-cloud-attachment-sdk/src/main/resources/attachment-sdk-cofiguration.properties b/mall-ebtp-cloud-attachment-sdk/src/main/resources/attachment-sdk-cofiguration.properties index 9aec7c2..2da3758 100644 --- a/mall-ebtp-cloud-attachment-sdk/src/main/resources/attachment-sdk-cofiguration.properties +++ b/mall-ebtp-cloud-attachment-sdk/src/main/resources/attachment-sdk-cofiguration.properties @@ -6,4 +6,5 @@ server.max-http-header-size=2048576 # document center id document.center.service.id=core-service-document-center +document.data.service.id=core-service-document-center document.center.ip-address=http://10.242.31.158:8806/doc/ \ No newline at end of file diff --git a/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/base/service/impl/BaseCacheUserServiceImpl.java b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/base/service/impl/BaseCacheUserServiceImpl.java index eca5ce8..9b97ae0 100644 --- a/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/base/service/impl/BaseCacheUserServiceImpl.java +++ b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/base/service/impl/BaseCacheUserServiceImpl.java @@ -2,15 +2,16 @@ package com.chinaunicom.mall.ebtp.common.base.service.impl; import java.util.Collection; import java.util.Map; +import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; import org.springframework.beans.BeanUtils; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; -import org.springframework.util.Assert; import com.chinaunicom.mall.ebtp.common.base.entity.BaseCacheUser; import com.chinaunicom.mall.ebtp.common.base.service.IBaseCacheUserService; +import com.chinaunicom.mall.ebtp.common.exception.entity.InvalidTokenException; import lombok.extern.slf4j.Slf4j; @@ -29,19 +30,15 @@ public class BaseCacheUserServiceImpl implements IBaseCacheUserService { @Override public BaseCacheUser getCacheUser() { - try { - BaseCacheUser buser = new BaseCacheUser(); - BeanUtils.copyProperties(SecurityContextHolder.getContext().getAuthentication().getPrincipal(), buser); + BaseCacheUser buser = new BaseCacheUser(); + BeanUtils.copyProperties(SecurityContextHolder.getContext().getAuthentication().getPrincipal(), buser); - Assert.notNull(buser.getUserId(), "获取用户信息失败,无效的token!"); - - log.debug("Current user principal: " + buser); - - return buser; - } catch (Exception e) { - log.error(e.getMessage()); - throw e; + if (Objects.isNull(buser.getUserId())) { + throw new InvalidTokenException("用户验证失败,无效的token!"); } + log.debug("Current user principal: " + buser); + + return buser; } @Override diff --git a/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/exception/entity/InvalidTokenException.java b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/exception/entity/InvalidTokenException.java new file mode 100644 index 0000000..b5a74f1 --- /dev/null +++ b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/exception/entity/InvalidTokenException.java @@ -0,0 +1,27 @@ +package com.chinaunicom.mall.ebtp.common.exception.entity; + +/** + * 无效的token + * + * @author Ajaxfan + */ +public class InvalidTokenException extends RuntimeException { + + private static final long serialVersionUID = 7870600175887301004L; + + public InvalidTokenException() { + } + + public InvalidTokenException(final String message, final Throwable cause) { + super(message, cause); + } + + public InvalidTokenException(final Throwable cause) { + super(cause); + } + + public InvalidTokenException(final String message) { + super(message); + } + +} diff --git a/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/exception/service/BusinessExceptionHandlerAdviceDefault.java b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/exception/service/BusinessExceptionHandlerAdviceDefault.java index 5a2f137..a3524ad 100644 --- a/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/exception/service/BusinessExceptionHandlerAdviceDefault.java +++ b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/exception/service/BusinessExceptionHandlerAdviceDefault.java @@ -30,6 +30,7 @@ import org.springframework.web.method.annotation.MethodArgumentTypeMismatchExcep import com.chinaunicom.mall.ebtp.common.base.entity.BaseResponse; import com.chinaunicom.mall.ebtp.common.exception.entity.BusinessException; +import com.chinaunicom.mall.ebtp.common.exception.entity.InvalidTokenException; import com.chinaunicom.mall.ebtp.common.util.JsonUtils; import cn.hutool.core.convert.Convert; @@ -130,6 +131,24 @@ public class BusinessExceptionHandlerAdviceDefault { return BaseResponse.fail(HttpStatus.BAD_REQUEST.value(), "参数错误", Convert.toStr(body)); } + /** + * 用户验证失败,无效的token + * + * @param request 请求 + * @param exception InvalidTokenException异常对象 + * @return 响应 + */ + @ExceptionHandler(InvalidTokenException.class) + @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED) + public BaseResponse handleInvalidTokenException(HttpServletRequest request, + InvalidTokenException exception) { + Map body = new HashMap<>(); + body.put("errors", exception.getMessage()); + body.put("error", HttpStatus.UNAUTHORIZED.getReasonPhrase()); +// body.put("path", request.getRequestURI()); + return BaseResponse.fail(HttpStatus.UNAUTHORIZED.value(), "用户验证失败,无效的token!", Convert.toStr(body)); + } + /** * 请求方式异常 * @@ -258,6 +277,11 @@ public class BusinessExceptionHandlerAdviceDefault { return BaseResponse.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), "系统异常", Convert.toStr(body)); } + /** + * @param request + * @param exception + * @return + */ @ExceptionHandler({ TransactionSystemException.class, RmTransactionException.class }) @ResponseStatus(HttpStatus.BAD_REQUEST) public BaseResponse handleTransactionSystemException(HttpServletRequest request, diff --git a/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/exception/service/BusinessExceptionHandlerAdvicePro.java b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/exception/service/BusinessExceptionHandlerAdvicePro.java index 381a12d..a9dfa0d 100644 --- a/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/exception/service/BusinessExceptionHandlerAdvicePro.java +++ b/uboot-common/src/main/java/com/chinaunicom/mall/ebtp/common/exception/service/BusinessExceptionHandlerAdvicePro.java @@ -30,6 +30,7 @@ import org.springframework.web.method.annotation.MethodArgumentTypeMismatchExcep import com.chinaunicom.mall.ebtp.common.base.entity.BaseResponse; import com.chinaunicom.mall.ebtp.common.exception.entity.BusinessException; +import com.chinaunicom.mall.ebtp.common.exception.entity.InvalidTokenException; import com.chinaunicom.mall.ebtp.common.util.JsonUtils; import cn.hutool.core.convert.Convert; @@ -146,6 +147,24 @@ public class BusinessExceptionHandlerAdvicePro { return BaseResponse.fail(HttpStatus.METHOD_NOT_ALLOWED.value(), "错误的请求方式", Convert.toStr(body)); } + /** + * 用户验证失败,无效的token + * + * @param request 请求 + * @param exception InvalidTokenException异常对象 + * @return 响应 + */ + @ExceptionHandler(InvalidTokenException.class) + @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED) + public BaseResponse handleInvalidTokenException(HttpServletRequest request, + InvalidTokenException exception) { + Map body = new HashMap<>(); + body.put("errors", exception.getMessage()); + body.put("error", HttpStatus.UNAUTHORIZED.getReasonPhrase()); +// body.put("path", request.getRequestURI()); + return BaseResponse.fail(HttpStatus.UNAUTHORIZED.value(), "用户验证失败,无效的token!", Convert.toStr(body)); + } + /** * 参数缺失 *