From eb0a96358e26a85661e4f63ac527395941e6b142 Mon Sep 17 00:00:00 2001 From: haoxr <1490493387@qq.com> Date: Mon, 16 Dec 2024 16:58:56 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=94=A8=E6=88=B7=E5=90=8D=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E6=8F=90=E7=A4=BA=E4=B8=8D=E6=AD=A3=E7=A1=AE=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/MyAuthenticationEntryPoint.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/youlai/boot/core/security/exception/MyAuthenticationEntryPoint.java b/src/main/java/com/youlai/boot/core/security/exception/MyAuthenticationEntryPoint.java index 9f3c144e..ddcae7d4 100644 --- a/src/main/java/com/youlai/boot/core/security/exception/MyAuthenticationEntryPoint.java +++ b/src/main/java/com/youlai/boot/core/security/exception/MyAuthenticationEntryPoint.java @@ -4,36 +4,37 @@ import com.youlai.boot.common.result.ResultCode; import com.youlai.boot.common.util.ResponseUtils; import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.core.AuthenticationException; +import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.web.AuthenticationEntryPoint; import org.springframework.stereotype.Component; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; + import java.io.IOException; /** * 认证异常处理 * - * @author haoxr + * @author Ray.Hao * @since 2.0.0 */ @Component public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint { - @Override + @Override public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { int status = response.getStatus(); if (status == HttpServletResponse.SC_NOT_FOUND) { // 资源不存在 - ResponseUtils.writeErrMsg(response, ResultCode.RESOURCE_NOT_FOUND); + ResponseUtils.writeErrMsg(response, ResultCode.USER_RESOURCE_NOT_FOUND); } else { - - if(authException instanceof BadCredentialsException){ + if (authException instanceof UsernameNotFoundException || authException instanceof BadCredentialsException) { // 用户名或密码错误 - ResponseUtils.writeErrMsg(response, ResultCode.USERNAME_OR_PASSWORD_ERROR); - }else { + ResponseUtils.writeErrMsg(response, ResultCode.USER_PASSWORD_ERROR); + } else { // 未认证或者token过期 - ResponseUtils.writeErrMsg(response, ResultCode.TOKEN_INVALID); + ResponseUtils.writeErrMsg(response, ResultCode.ACCESS_TOKEN_INVALID); } } }