springSecurity与Vue项目 重定向404问题

我配置了一个springSecurity,准备了一个测试接口/test,这个接口是需要认证的。当我在浏览器直接访问/test的时候,会被识别为无认证并跳转到我指定的vue登录页面。但当我通过vue中的axios请求去访问这个/test接口的时候,则会出现404问题,请问这是为什么?

这是security配置

img

这是在浏览器中访问/test接口,成功

img

这是通过axios发起/test请求,重定向到登录页面的时候发生404,请问是什么原因?

img

img

普通的请求和ajax请求不太一样,使用ajax请求失败,会把登录页面当作数据返回给ajax请求。
你试试实现SpringSecurity的这个类,来给ajax请求返回错误信息,然后ajax请求再转到登录页面:


public interface AuthenticationFailureHandler {

    /**
     * Called when an authentication attempt fails.
     * @param request the request during which the authentication attempt occurred.
     * @param response the response.
     * @param exception the exception which was thrown to reject the authentication
     * request.
     */
    void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
            AuthenticationException exception) throws IOException, ServletException;

}

有兴趣可以看看SpringSecurity的文章https://blog.csdn.net/lookoutthe/category_11588763.html