增加token拦截日志输出

This commit is contained in:
ajaxfan
2021-04-21 14:40:19 +08:00
parent 61bc834ea7
commit b7a2b94bde
6 changed files with 39 additions and 36 deletions

View File

@ -33,7 +33,7 @@ public class BaseCacheUserServiceImpl implements IBaseCacheUserService {
BeanUtils.copyProperties(SecurityContextHolder.getContext().getAuthentication().getPrincipal(), buser);
// if (Objects.isNull(buser.getUserId())) {
// throw new InvalidTokenException("用户验证失败无效的token!");
// throw new AuthFailureException("登陆已超期");
// }
log.debug("Current user principal: " + buser);
} catch (Exception e) {

View File

@ -0,0 +1,27 @@
package com.chinaunicom.mall.ebtp.common.exception.entity;
/**
* 无效的token
*
* @author Ajaxfan
*/
public class AuthFailureException extends RuntimeException {
private static final long serialVersionUID = 7870600175887301004L;
public AuthFailureException() {
}
public AuthFailureException(final String message, final Throwable cause) {
super(message, cause);
}
public AuthFailureException(final Throwable cause) {
super(cause);
}
public AuthFailureException(final String message) {
super(message);
}
}

View File

@ -1,27 +0,0 @@
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,7 +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.exception.entity.AuthFailureException;
import com.chinaunicom.mall.ebtp.common.util.JsonUtils;
import cn.hutool.core.convert.Convert;
@ -138,15 +138,15 @@ public class BusinessExceptionHandlerAdviceDefault {
* @param exception InvalidTokenException异常对象
* @return 响应
*/
@ExceptionHandler(InvalidTokenException.class)
@ExceptionHandler(AuthFailureException.class)
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
public BaseResponse<String> handleInvalidTokenException(HttpServletRequest request,
InvalidTokenException exception) {
AuthFailureException 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));
return BaseResponse.fail(HttpStatus.UNAUTHORIZED.value(), "登陆已超期", Convert.toStr(body));
}
/**

View File

@ -30,7 +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.exception.entity.AuthFailureException;
import com.chinaunicom.mall.ebtp.common.util.JsonUtils;
import cn.hutool.core.convert.Convert;
@ -154,15 +154,15 @@ public class BusinessExceptionHandlerAdvicePro {
* @param exception InvalidTokenException异常对象
* @return 响应
*/
@ExceptionHandler(InvalidTokenException.class)
@ExceptionHandler(AuthFailureException.class)
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
public BaseResponse<String> handleInvalidTokenException(HttpServletRequest request,
InvalidTokenException exception) {
AuthFailureException 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));
return BaseResponse.fail(HttpStatus.UNAUTHORIZED.value(), "登陆已超期", Convert.toStr(body));
}
/**