fix: 用户名错误提示不正确问题修复

This commit is contained in:
haoxr
2024-12-16 16:58:56 +08:00
parent e8b97bc9c8
commit eb0a96358e

View File

@@ -4,18 +4,20 @@ import com.youlai.boot.common.result.ResultCode;
import com.youlai.boot.common.util.ResponseUtils; import com.youlai.boot.common.util.ResponseUtils;
import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.AuthenticationEntryPoint; import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import jakarta.servlet.ServletException; import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
/** /**
* 认证异常处理 * 认证异常处理
* *
* @author haoxr * @author Ray.Hao
* @since 2.0.0 * @since 2.0.0
*/ */
@Component @Component
@@ -25,15 +27,14 @@ public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {
int status = response.getStatus(); int status = response.getStatus();
if (status == HttpServletResponse.SC_NOT_FOUND) { if (status == HttpServletResponse.SC_NOT_FOUND) {
// 资源不存在 // 资源不存在
ResponseUtils.writeErrMsg(response, ResultCode.RESOURCE_NOT_FOUND); ResponseUtils.writeErrMsg(response, ResultCode.USER_RESOURCE_NOT_FOUND);
} else { } else {
if (authException instanceof UsernameNotFoundException || authException instanceof BadCredentialsException) {
if(authException instanceof BadCredentialsException){
// 用户名或密码错误 // 用户名或密码错误
ResponseUtils.writeErrMsg(response, ResultCode.USERNAME_OR_PASSWORD_ERROR); ResponseUtils.writeErrMsg(response, ResultCode.USER_PASSWORD_ERROR);
}else { } else {
// 未认证或者token过期 // 未认证或者token过期
ResponseUtils.writeErrMsg(response, ResultCode.TOKEN_INVALID); ResponseUtils.writeErrMsg(response, ResultCode.ACCESS_TOKEN_INVALID);
} }
} }
} }