From 80e1b9f6100675de2024bd36800b9082d9dd4253 Mon Sep 17 00:00:00 2001 From: wjie Date: Wed, 11 Aug 2021 09:39:28 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20Fixing=20a=20bug.=20closed=20#I44U16=20?= =?UTF-8?q?token=E5=A4=B1=E6=95=88=E5=90=8E=20=E6=B2=A1=E6=9C=89json?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E8=BF=94=E5=9B=9E=20=E8=80=8C=E6=98=AF?= =?UTF-8?q?=E6=8A=A5500?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PigWebResponseExceptionTranslator.java | 3 +- .../ResourceAuthExceptionEntryPoint.java | 11 ++++- .../exception/TokenInvalidException.java | 47 +++++++++++++++++++ 3 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/exception/TokenInvalidException.java diff --git a/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigWebResponseExceptionTranslator.java b/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigWebResponseExceptionTranslator.java index eba6e75b..c12ee7d6 100755 --- a/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigWebResponseExceptionTranslator.java +++ b/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigWebResponseExceptionTranslator.java @@ -65,10 +65,11 @@ public class PigWebResponseExceptionTranslator implements WebResponseExceptionTr return handleOAuth2Exception(new InvalidException(ase.getMessage(), ase)); } + // token 过期 特殊处理 返回 424 不是 401 ase = (InvalidTokenException) throwableAnalyzer.getFirstThrowableOfType(InvalidTokenException.class, causeChain); if (ase != null) { - return handleOAuth2Exception(new UnauthorizedException(ase.getMessage(), ase)); + return handleOAuth2Exception(new TokenInvalidException(ase.getMessage(), ase)); } ase = (HttpRequestMethodNotSupportedException) throwableAnalyzer diff --git a/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/ResourceAuthExceptionEntryPoint.java b/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/ResourceAuthExceptionEntryPoint.java index 9b8bc544..b9144716 100755 --- a/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/ResourceAuthExceptionEntryPoint.java +++ b/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/ResourceAuthExceptionEntryPoint.java @@ -22,6 +22,7 @@ import com.pig4cloud.pig.common.core.constant.CommonConstants; import com.pig4cloud.pig.common.core.util.R; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; +import org.springframework.security.authentication.InsufficientAuthenticationException; import org.springframework.security.core.AuthenticationException; import org.springframework.security.web.AuthenticationEntryPoint; @@ -45,12 +46,18 @@ public class ResourceAuthExceptionEntryPoint implements AuthenticationEntryPoint response.setCharacterEncoding(CommonConstants.UTF8); response.setContentType(CommonConstants.CONTENT_TYPE); R result = new R<>(); - result.setCode(HttpStatus.HTTP_UNAUTHORIZED); + result.setCode(CommonConstants.FAIL); + response.setStatus(HttpStatus.HTTP_UNAUTHORIZED); if (authException != null) { result.setMsg("error"); result.setData(authException.getMessage()); } - response.setStatus(HttpStatus.HTTP_UNAUTHORIZED); + + // 针对令牌过期返回特殊的 424 + if (authException instanceof InsufficientAuthenticationException) { + response.setStatus(org.springframework.http.HttpStatus.FAILED_DEPENDENCY.value()); + result.setMsg("token expire"); + } PrintWriter printWriter = response.getWriter(); printWriter.append(objectMapper.writeValueAsString(result)); } diff --git a/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/exception/TokenInvalidException.java b/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/exception/TokenInvalidException.java new file mode 100644 index 00000000..49256945 --- /dev/null +++ b/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/exception/TokenInvalidException.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pig.common.security.exception; + +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.pig4cloud.pig.common.security.component.PigAuth2ExceptionSerializer; +import org.springframework.http.HttpStatus; + +/** + * @author lengleng + * @date 2021-08-05 + *

+ * 令牌不合法 + */ +@JsonSerialize(using = PigAuth2ExceptionSerializer.class) +public class TokenInvalidException extends PigAuth2Exception { + + public TokenInvalidException(String msg, Throwable t) { + super(msg); + } + + @Override + public String getOAuth2ErrorCode() { + return "invalid_token"; + } + + @Override + public int getHttpErrorCode() { + return HttpStatus.FAILED_DEPENDENCY.value(); + } + +} -- GitLab