增加token验证失败异常信息

This commit is contained in:
ajaxfan
2021-04-21 09:44:39 +08:00
parent 88170027a6
commit 0966f4c4ef
7 changed files with 124 additions and 12 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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<String> handleInvalidTokenException(HttpServletRequest request,
InvalidTokenException exception) {
Map<String, Object> 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<String> handleTransactionSystemException(HttpServletRequest request,

View File

@ -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<String> handleInvalidTokenException(HttpServletRequest request,
InvalidTokenException exception) {
Map<String, Object> 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));
}
/**
* 参数缺失
*