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 eba6e75b35815f78163330be6b201b596b9d0285..c12ee7d6b01593a59a678a12db602ee729695eae 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 9b8bc54447db75578f7784544f7a02824e587204..b9144716fe5f34234c8bd0254c49247be39ffcfa 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 0000000000000000000000000000000000000000..49256945d1bde313c131586371824603e9f0ae2a --- /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(); + } + +}