创建handle=》MyAuthenticationFailureHandle.java

/**
 * @author etern
 * @title: MyAuthenticationFailureHandle
 * @projectName springsecuritydemo
 * @description: TODO
 * @date 2022/1/21 23:51
 */
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {

    private String url;

    public MyAuthenticationFailureHandler(String url) {
        this.url = url;
    }

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        response.sendRedirect(url);
    }
}

SecurityConfig.java

//登录失败后处理器,不能和failureForwardUrl共存
.failureHandler(new MyAuthenticationFailureHandler("/error.html"));

位置截图: