import com.shareaccount.backend.enums.ResponseCodeEnum;  
import com.shareaccount.backend.util.Response;  
import com.shareaccount.backend.util.ResultUtil;  
import jakarta.servlet.ServletException;  
import jakarta.servlet.http.HttpServletRequest;  
import jakarta.servlet.http.HttpServletResponse;  
import lombok.extern.slf4j.Slf4j;  
import org.springframework.security.authentication.BadCredentialsException;  
import org.springframework.security.core.AuthenticationException;  
import org.springframework.security.core.userdetails.UsernameNotFoundException;  
import org.springframework.security.web.authentication.AuthenticationFailureHandler;  
import org.springframework.stereotype.Component;  
  
import java.io.IOException;  
  
@Component  
@Slf4j  
public class RestAuthenticationFailureHandler implements AuthenticationFailureHandler {  
    @Override  
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {  
        log.warn("AuthenticationException: ", exception);  
        if (exception instanceof UsernameNotFoundException) {  
            // 用户名或密码为空  
            ResultUtil.fail(response, Response.fail(exception.getMessage()));  
        } else if (exception instanceof BadCredentialsException) {  
            // 用户名或密码错误  
            ResultUtil.fail(response, Response.fail(ResponseCodeEnum.USERNAME_OR_PWD_ERROR));  
        }  
        // 登录失败  
        ResultUtil.fail(response, Response.fail(ResponseCodeEnum.LOGIN_FAIL));  
    }}