如何在 jQuery Ajax 调用后管理重定向请求

How to manage a redirect request after a jQuery Ajax call

本文关键字:管理 重定向 请求 调用 Ajax jQuery      更新时间:2023-09-26

我正在使用$.post()来调用Ajax的servlet,然后使用生成的HTML片段来替换用户当前页面中的div元素。但是,如果会话超时,服务器将发送重定向指令以将用户发送到登录页面。在这种情况下,jQuery正在用登录页面的内容替换div元素,迫使用户的眼睛目睹一个罕见的场景。

如何使用 jQuery 1.2.6 管理来自 Ajax 调用的重定向指令?

我阅读了这个问题并实现了有关将响应HTTP状态代码设置为278的方法,以避免浏览器透明地处理重定向。尽管这奏效了,但我有点不满意,因为它有点黑客。

经过更多的挖掘,我放弃了这种方法并使用了JSON。在这种情况下,对 AJAX 请求的所有响应的状态代码为 200,响应正文包含在服务器上构造的 JSON 对象。然后,客户端上的 JavaScript 可以使用 JSON 对象来决定它需要做什么。

我有一个和你类似的问题。我执行一个 AJAX 请求,该请求有 2 个可能的响应:一个将浏览器重定向到新页面,另一个将当前页面上的现有 HTML 表单替换为新表单。执行此操作的 jQuery 代码如下所示:

$.ajax({
    type: "POST",
    url: reqUrl,
    data: reqBody,
    dataType: "json",
    success: function(data, textStatus) {
        if (data.redirect) {
            // data.redirect contains the string URL to redirect to
            window.location.href = data.redirect;
        } else {
            // data.form contains the HTML for the replacement form
            $("#myform").replaceWith(data.form);
        }
    }
});

JSON 对象"data"在服务器上构造为具有 2 个成员:data.redirectdata.form 。我发现这种方法要好得多。

我通过以下方式解决了这个问题:

  1. 向响应添加自定义标头:

    public ActionResult Index(){
        if (!HttpContext.User.Identity.IsAuthenticated)
        {
            HttpContext.Response.AddHeader("REQUIRES_AUTH","1");
        }
        return View();
    }
    
  2. 将 JavaScript 函数绑定到 ajaxSuccess 事件并检查标头是否存在:

    $(document).ajaxSuccess(function(event, request, settings) {
        if (request.getResponseHeader('REQUIRES_AUTH') === '1') {
           window.location = '/';
        }
    });
    

没有浏览器可以正确处理 301 和 302 响应。事实上,该标准甚至说他们应该"透明地"处理它们,这对Ajax库供应商来说是一个巨大的头痛。在 Ra-Ajax 中,我们被迫使用 HTTP 响应状态代码 278(只是一些"未使用"的成功代码(来处理来自服务器的透明重定向......

这真的让我很恼火,如果这里有人在 W3C 中有一些"拉动",我希望你能让 W3C 知道我们真的需要自己处理 301 和 302 代码......! ;)

最终实现的解决方案是将包装器用于 Ajax 调用的回调函数,并在此包装器中检查返回的 HTML 块上是否存在特定元素。如果找到该元素,则包装器执行重定向。如果没有,包装器将调用转发到实际的回调函数。

例如,我们的包装函数是这样的:

function cbWrapper(data, funct){
    if($("#myForm", data).length > 0)
        top.location.href="login.htm";//redirection
    else
        funct(data);
}

然后,在进行 Ajax 调用时,我们使用了类似的东西:

$.post("myAjaxHandler", 
       {
        param1: foo,
        param2: bar
       },
       function(data){
           cbWrapper(data, myActualCB);
       }, 
       "html"
);

这对我们有用,因为所有 Ajax 调用总是在我们用来替换页面片段的 DIV 元素中返回 HTML。此外,我们只需要重定向到登录页面。

我喜欢Timmerz的方法,略带柠檬味。如果您在期待 JSON 时收到返回的内容文本/html类型,则很可能被重定向。就我而言,我只是简单地重新加载页面,然后它就会被重定向到登录页面。哦,检查jqXHR状态是否为200,这看起来很愚蠢,因为你在错误函数中,对吧?否则,合法的错误情况将强制迭代重新加载(糟糕(

$.ajax(
   error:  function (jqXHR, timeout, message) {
    var contentType = jqXHR.getResponseHeader("Content-Type");
    if (jqXHR.status === 200 && contentType.toLowerCase().indexOf("text/html") >= 0) {
        // assume that our login has expired - reload our current page
        window.location.reload();
    }
});

使用低级$.ajax()调用:

$.ajax({
  url: "/yourservlet",
  data: { },
  complete: function(xmlHttp) {
    // xmlHttp is a XMLHttpRquest object
    alert(xmlHttp.status);
  }
});

尝试此操作以进行重定向:

if (xmlHttp.code != 200) {
  top.location.href = '/some/other/page';
}

我只是想分享我的方法,因为这可能会对某人有所帮助:

我基本上包含一个JavaScript模块,该模块处理身份验证内容,例如显示用户名以及处理重定向到登录页面的情况。

我的场景:我们基本上有一个ISA服务器,它侦听所有请求,并使用302和位置标头响应我们的登录页面。

在我的JavaScript模块中,我最初的方法是这样的

$(document).ajaxComplete(function(e, xhr, settings){
    if(xhr.status === 302){
        //check for location header and redirect...
    }
});

问题(正如这里已经提到的许多人(是浏览器自行处理重定向,因此我的ajaxComplete回调从未被调用,而是我得到了已经重定向的登录页面的响应,这显然是一个status 200。问题:如何检测成功的 200 响应是您的实际登录页面还是只是其他任意页面?

解决方案

由于我无法捕获 302 重定向响应,因此我在登录页面上添加了一个 LoginPage 标头,其中包含登录页面本身的 URL。在模块中,我现在侦听标头并执行重定向:

if(xhr.status === 200){
    var loginPageRedirectHeader = xhr.getResponseHeader("LoginPage");
    if(loginPageRedirectHeader && loginPageRedirectHeader !== ""){
        window.location.replace(loginPageRedirectHeader);
    }
}

。这就像魅力:)一样。您可能想知道为什么我将 url 包含在 LoginPage 标题中......好吧,基本上是因为我发现无法确定从xhr对象自动位置重定向产生的GET的 url......

我知道

这个话题很旧,但我会给出我在这里找到并之前描述的另一种方法。基本上我正在使用ASP。带有WIF的MVC(但这对于本主题的上下文并不重要 - 无论使用哪种框架,答案都足够。线索保持不变 - 在执行 ajax 请求时处理与身份验证失败相关的问题(。

下面显示的方法可以应用于所有开箱即用的 ajax 请求(如果它们显然没有重新定义 beforeSend 事件(。

$.ajaxSetup({
    beforeSend: checkPulse,
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        document.open();
        document.write(XMLHttpRequest.responseText);
        document.close();
    }
});

在执行任何 ajax 请求之前CheckPulse调用方法(控制器方法可以是最简单的方法(:

[Authorize]
public virtual void CheckPulse() {}

如果用户未经过身份验证(令牌已过期(,则无法访问此类方法(受Authorize属性保护(。由于框架处理身份验证,因此当令牌过期时,它会将 http 状态 302 放入响应中。如果您不希望浏览器透明地处理 302 响应,请在 Global.asax 中捕获它并更改响应状态 - 例如更改为 200 OK。 此外,添加标头,它指示您以特殊方式处理此类响应(稍后在客户端(:

protected void Application_EndRequest()
{
    if (Context.Response.StatusCode == 302
        && (new HttpContextWrapper(Context)).Request.IsAjaxRequest())
    {                
        Context.Response.StatusCode = 200;
        Context.Response.AddHeader("REQUIRES_AUTH", "1");
    }
}

最后在客户端检查此类自定义标头。如果存在 - 应该完全重定向到登录页面(在我的情况下window.location被我的框架自动处理的请求中的 url 替换(。

function checkPulse(XMLHttpRequest) {
    var location = window.location.href;
    $.ajax({
        url: "/Controller/CheckPulse",
        type: 'GET',
        async: false,
        beforeSend: null,
        success:
            function (result, textStatus, xhr) {
                if (xhr.getResponseHeader('REQUIRES_AUTH') === '1') {
                    XMLHttpRequest.abort(); // terminate further ajax execution
                    window.location = location;
                }
            }
    });
}
我认为

处理此问题的更好方法是利用现有的HTTP协议响应代码,特别是401 Unauthorized

这是我解决它的方法:

  1. 服务器端:如果会话过期,并且请求是 ajax。 发送 401 响应代码标头
  2. 客户端:绑定到 ajax 事件

    $('body').bind('ajaxSuccess',function(event,request,settings){
    if (401 == request.status){
        window.location = '/users/login';
    }
    }).bind('ajaxError',function(event,request,settings){
    if (401 == request.status){
        window.location = '/users/login';
    }
    });
    

IMO 这是更通用的,您没有编写一些新的自定义规范/标头。 您也不必修改任何现有的 ajax 调用。

编辑:根据@Rob下面的评论,401(身份验证错误的HTTP状态代码(应该是指示器。有关更多详细信息,请参阅 403 禁止访问与 401 未经授权的 HTTP 响应。话虽如此,一些 Web 框架对身份验证和授权错误都使用 403 - 因此请相应地进行调整。谢谢罗布。

我像这样解决了这个问题:

添加中间件以处理响应,如果是 ajax 请求的重定向,请将响应更改为具有重定向 url 的正常响应。

class AjaxRedirect(object):
  def process_response(self, request, response):
    if request.is_ajax():
      if type(response) == HttpResponseRedirect:
        r = HttpResponse(json.dumps({'redirect': response['Location']}))
        return r
    return response

然后在 ajaxComplete 中,如果响应包含重定向,则它必须是重定向,因此请更改浏览器的位置。

$('body').ajaxComplete(function (e, xhr, settings) {
   if (xhr.status == 200) {
       var redirect = null;
       try {
           redirect = $.parseJSON(xhr.responseText).redirect;
           if (redirect) {
               window.location.href = redirect.replace(/'?.*$/, "?next=" + window.location.pathname);
           }
       } catch (e) {
           return;
       }
   }
}

我发现的另一个解决方案(如果要设置全局行为,则特别有用(是将$.ajaxsetup()方法与statusCode属性一起使用。正如其他人指出的那样,不要使用重定向状态代码(3xx(,而是使用4xx状态代码并处理重定向客户端。

$.ajaxSetup({ 
  statusCode : {
    400 : function () {
      window.location = "/";
    }
  }
});

400替换为要处理的状态代码。就像已经提到的401 Unauthorized可能是个好主意。我使用400因为它非常不具体,我可以将401用于更具体的情况(例如错误的登录凭据(。因此,当会话超时时,您的后端应该返回一个4xx错误代码,而不是直接重定向,并且您处理重定向客户端。即使使用像 Backbone 这样的框架,对我来说也很完美.js

我有一个简单的解决方案,不需要更改服务器代码......只需添加一茶匙肉豆蔻...

$(document).ready(function ()
{
    $(document).ajaxSend(
    function(event,request,settings)
    {
        var intercepted_success = settings.success;
        settings.success = function( a, b, c ) 
        {  
            if( request.responseText.indexOf( "<html>" ) > -1 )
                window.location = window.location;
            else
                intercepted_success( a, b, c );
        };
    });
});

我检查了 html 标签的存在,但您可以更改 indexOf 以搜索登录页面中存在的任何唯一字符串......

大多数给定的解决方案都使用解决方法,使用额外的标头或不适当的HTTP代码。这些解决方案很可能会起作用,但感觉有点"笨拙"。我想出了另一个解决方案。

我们使用配置为在

401 响应上重定向(被动重定向启用="true"(的 WIF。重定向在处理正常请求时很有用,但不适用于 AJAX 请求(因为浏览器不会执行 302/重定向(。

在 global.asax 中使用以下代码,可以禁用 AJAX 请求的重定向:

    void WSFederationAuthenticationModule_AuthorizationFailed(object sender, AuthorizationFailedEventArgs e)
    {
        string requestedWithHeader = HttpContext.Current.Request.Headers["X-Requested-With"];
        if (!string.IsNullOrEmpty(requestedWithHeader) && requestedWithHeader.Equals("XMLHttpRequest", StringComparison.OrdinalIgnoreCase))
        {
            e.RedirectToIdentityProvider = false;
        }
    }

这允许您返回 AJAX 请求的 401 响应,然后您的 javascript 可以通过重新加载页面来处理这些响应。重新加载页面将抛出一个 401,该 401 将由 WIF 处理(WIF 会将用户重定向到登录页面(。

处理 401 错误的 JavaScript 示例:

$(document).ajaxError(function (event, jqxhr, settings, exception) {
    if (jqxhr.status == 401) { //Forbidden, go to login
        //Use a reload, WIF will redirect to Login
        location.reload(true);
    }
});

使用 MVC 重定向到操作方法可能会出现此问题 ASP.NET 然后。为了防止表单在div 中显示响应,您可以简单地使用 $.ajaxSetup 执行某种 ajax 响应过滤器来传入响应。如果响应包含 MVC 重定向,则可以在 JS 端计算此表达式。JS的示例代码如下:

$.ajaxSetup({
    dataFilter: function (data, type) {
        if (data && typeof data == "string") {
            if (data.indexOf('window.location') > -1) {
                eval(data);
            }
        }
        return data;
    }
});

如果数据是:">window.location = '/Acount/Login'"上面的过滤器将捕获它并评估以进行重定向,而不是让数据显示数据。

把弗拉基米尔·普鲁德尼科夫和托马斯·汉森的话放在一起:

  • 更改服务器端代码以检测它是否为 XHR。如果是,请将重定向的响应代码设置为 278。在姜戈中:
   if request.is_ajax():
      response.status_code = 278

这使得浏览器将响应视为成功,并将其交给您的Javascript。

  • 在您的 JS 中,确保表单提交是通过 Ajax 进行的,检查响应代码并在需要时重定向:
$('#my-form').submit(function(event){ 
  event.preventDefault();   
  var options = {
    url: $(this).attr('action'),
    type: 'POST',
    complete: function(response, textStatus) {    
      if (response.status == 278) { 
        window.location = response.getResponseHeader('Location')
      }
      else { ... your code here ... } 
    },
    data: $(this).serialize(),   
  };   
  $.ajax(options); 
});
    <script>
    function showValues() {
        var str = $("form").serialize();
        $.post('loginUser.html', 
        str,
        function(responseText, responseStatus, responseXML){
            if(responseStatus=="success"){
                window.location= "adminIndex.html";
            }
        });     
    }
</script>

让我再次引用@Steg描述的问题

我有一个和你类似的问题。我执行一个有 2 个的 ajax 请求 可能的响应:将浏览器重定向到新页面的响应,以及 一个将当前页面上的现有 HTML 表单替换为新的 HTML 表单 一。

恕我直言,这是一个真正的挑战,必须正式扩展到当前的HTTP标准。

我相信新的 Http 标准将使用新的状态代码。含义:当前301/302告诉浏览器去获取请求的内容到新location

在扩展标准中,它会说如果响应status: 308(只是一个示例(,那么浏览器应该将主页重定向到提供的location

话虽如此;我倾向于已经模仿这种未来的行为,因此当需要 document.redirect 时,我让服务器响应为:

status: 204 No Content
x-status: 308 Document Redirect
x-location: /login.html

当JS获取" status: 204"时,它会检查x-status: 308标头是否存在,并执行文档重定向到location标头中提供的页面。

这对你有什么意义吗?

尝试

    $(document).ready(function () {
        if ($("#site").length > 0) {
            window.location = "<%= Url.Content("~") %>" + "Login/LogOn";
        }
    });

将其放在登录页面上。如果它是在主页上的div 中加载的,它将重定向到登录页面。"#site"是div 的 ID,位于除登录页面以外的所有页面上。

虽然答案似乎对使用Spring Security的人有用,但我发现扩展LoginUrlAuthenticationEntryPoint并添加特定代码来处理AJAX更健壮。 大多数示例拦截所有重定向,而不仅仅是身份验证失败。 这对于我从事的项目来说是不可取的。 如果您不希望缓存失败的 AJAX 请求,您可能会发现还需要扩展 ExceptionTranslationFilter 并覆盖"sendStartAuthentication"方法以删除缓存步骤。

示例 AjaxAwareAuthenticationEntryPoint:

public class AjaxAwareAuthenticationEntryPoint extends
    LoginUrlAuthenticationEntryPoint {
    public AjaxAwareAuthenticationEntryPoint(String loginUrl) {
        super(loginUrl);
    }
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        if (isAjax(request)) {
            response.sendError(HttpStatus.UNAUTHORIZED.value(), "Please re-authenticate yourself");
        } else {
        super.commence(request, response, authException);
        }
    }
    public static boolean isAjax(HttpServletRequest request) {
        return request != null && "XMLHttpRequest".equals(request.getHeader("X-Requested-With"));
    }
}

来源:1, 2

我通过将以下内容放在我的登录.php页面中来解决这个问题。

<script type="text/javascript">
    if (top.location.href.indexOf('login.php') == -1) {
        top.location.href = '/login.php';
    }
</script>

有些人可能会发现以下内容很有用:

我希望客户端被重定向到登录页面,以便在没有授权令牌的情况下发送任何休息操作。由于我所有的 rest-actions 都是基于 Ajax 的,所以我需要一个很好的通用方法来重定向到登录页面,而不是处理 Ajax 成功函数。

这是我所做的:

在任何 Ajax 请求中,我的服务器将返回 Json 200 响应"需要身份验证"(如果客户端需要身份验证(。

Java 中的简单示例(服务器端(:

@Secured
@Provider
@Priority(Priorities.AUTHENTICATION)
public class AuthenticationFilter implements ContainerRequestFilter {
    private final Logger m_logger = LoggerFactory.getLogger(AuthenticationFilter.class);
    public static final String COOKIE_NAME = "token_cookie"; 
    @Override
    public void filter(ContainerRequestContext context) throws IOException {        
        // Check if it has a cookie.
        try {
            Map<String, Cookie> cookies = context.getCookies();
            if (!cookies.containsKey(COOKIE_NAME)) {
                m_logger.debug("No cookie set - redirect to login page");
                throw new AuthenticationException();
            }
        }
        catch (AuthenticationException e) {
            context.abortWith(Response.ok("'"NEED TO AUTHENTICATE'"").type("json/application").build());
        }
    }
}

在我的Javascript中,我添加了以下代码:

$.ajaxPrefilter(function(options, originalOptions, jqXHR) {
    var originalSuccess = options.success;
    options.success = function(data) {
        if (data == "NEED TO AUTHENTICATE") {
            window.location.replace("/login.html");
        }
        else {
            originalSuccess(data);
        }
    };      
});

仅此而已。

在 servlet 中,你应该把 response.setStatus(response.SC_MOVED_PERMANENTLY);要发送"301"xmlHttp 状态,您需要重定向...

在 $.ajax 函数中,你不应该使用 .toString() 函数...,只是

if (xmlHttp.status == 301) { top.location.href = 'xxxx.jsp'; }

问题是它不是很灵活,您无法决定要重定向的位置。

通过 servlet 重定向应该是最好的方法。 但我仍然找不到正确的方法。

我只是想抓住整个页面的任何 ajax 请求。 @SuperG让我开始了。这是我最终得到的:

// redirect ajax requests that are redirected, not found (404), or forbidden (403.)
$('body').bind('ajaxComplete', function(event,request,settings){
        switch(request.status) {
            case 301: case 404: case 403:                    
                window.location.replace("http://mysite.tld/login");
                break;
        }
});

我想专门检查某些http状态代码作为我的决定的基础。但是,您可以绑定到 ajaxError 以获得成功以外的任何内容(也许只有 200 个?我本可以写:

$('body').bind('ajaxError', function(event,request,settings){
    window.location.replace("http://mysite.tld/login");
}
<</div> div class="answers">

如果您还想传递值,那么您还可以设置会话变量并访问例如:在你的 jsp 中,你可以写

<% HttpSession ses = request.getSession(true);
   String temp=request.getAttribute("what_you_defined"); %>
然后

你可以把这个临时值存储在你的javascript变量中,然后玩一玩

我在标头解决方案上没有任何成功 - 它们从未在我的 ajaxSuccess/ajaxComplete 方法中被拾取。我在自定义响应中使用了 Steg 的答案,但我对 JS 端进行了一些修改。我设置了一个在每个函数中调用的方法,以便可以使用标准$.get$.post方法。

function handleAjaxResponse(data, callback) {
    //Try to convert and parse object
    try {
        if (jQuery.type(data) === "string") {
            data = jQuery.parseJSON(data);
        }
        if (data.error) {
            if (data.error == 'login') {
                window.location.reload();
                return;
            }
            else if (data.error.length > 0) {
                alert(data.error);
                return;
            }
        }
    }
    catch(ex) { }
    if (callback) {
        callback(data);
    }
}

使用中的示例...

function submitAjaxForm(form, url, action) {
    //Lock form
    form.find('.ajax-submit').hide();
    form.find('.loader').show();
    $.post(url, form.serialize(), function (d) {
        //Unlock form
        form.find('.ajax-submit').show();
        form.find('.loader').hide();
        handleAjaxResponse(d, function (data) {
            // ... more code for if auth passes ...
        });
    });
    return false;
}

最后,我通过添加自定义HTTP Header来解决问题。在服务器端响应每个请求之前,我将当前请求的 url 添加到响应的标头中。

我在服务器上的应用程序类型是 Asp.Net MVC ,它有一个很好的位置来做到这一点。 Global.asax年,我实现了Application_EndRequest事件,因此:

    public class MvcApplication : System.Web.HttpApplication
    {
    //  ...
    //  ...
        protected void Application_EndRequest(object sender, EventArgs e)
        {
            var app = (HttpApplication)sender;
            app.Context.Response.Headers.Add("CurrentUrl",app.Context. Request.CurrentExecutionFilePath);
        }
    }

它非常适合我!现在,在JQuery $.post的每个响应中,我都有请求的url以及其他响应标头,这些标头是状态302 POST方法的结果,303 ,... .

其他重要的事情是,无需在服务器端或客户端修改代码。

接下来是能够以这种方式访问后期操作的其他信息,例如错误、消息和......。

我发布了这个,也许可以帮助某人:)

我在我正在修补的 django 应用程序上遇到了这个问题(免责声明:我正在修补学习,绝不是专家(。我想做的是使用 jQuery ajax 向资源发送 DELETE 请求,在服务器端将其删除,然后将重定向发送回(基本上(主页。当我从python脚本发送HttpResponseRedirect('/the-redirect/')时,jQuery的ajax方法接收200而不是302。所以,我所做的是发送300的回复:

response = HttpResponse(status='300')
response['Location'] = '/the-redirect/' 
return  response

然后我用jQuery.ajax在客户端上发送/处理请求,如下所示:

<button onclick="*the-jquery*">Delete</button>
where *the-jquery* =
$.ajax({ 
  type: 'DELETE', 
  url: '/resource-url/', 
  complete: function(jqxhr){ 
    window.location = jqxhr.getResponseHeader('Location'); 
  } 
});

也许使用 300 并不"正确",但至少它像我想要的那样工作。

PS :在SO的移动版本上进行编辑是一个巨大的痛苦。愚蠢的ISP在我完成回答后就把我的服务取消请求放了!

你也可以挂接XMLHttpRequest send prototype。这将适用于具有一个处理程序的所有发送(jQuery/dojo/etc(。

我编写此代码来处理 500 页过期错误,但它应该也可以捕获 200 重定向。准备 XMLHttpRequest onreadystatechange 关于 readyState 含义的维基百科条目。

// Hook XMLHttpRequest
var oldXMLHttpRequestSend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function() {
  //console.dir( this );
  this.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 500 && this.responseText.indexOf("Expired") != -1) {
      try {
        document.documentElement.innerHTML = this.responseText;
      } catch(error) {
        // IE makes document.documentElement read only
        document.body.innerHTML = this.responseText;
      }
    }
  };
  oldXMLHttpRequestSend.apply(this, arguments);
}

我使用来自@John和@Arpad链接和@RobWinch链接的答案得到了一个工作解决方案

我使用 Spring Security 3.2.9 和 jQuery 1.10.2。

扩展 Spring 的类以仅从 AJAX 请求中引起 4XX 响应:

public class CustomLoginUrlAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint {
    public CustomLoginUrlAuthenticationEntryPoint(final String loginFormUrl) {
        super(loginFormUrl);
    }
    // For AJAX requests for user that isn't logged in, need to return 403 status.
    // For normal requests, Spring does a (302) redirect to login.jsp which the browser handles normally.
    @Override
    public void commence(final HttpServletRequest request,
                         final HttpServletResponse response,
                         final AuthenticationException authException)
            throws IOException, ServletException {
        if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN, "Access Denied");
        } else {
            super.commence(request, response, authException);
        }
    }
}

applicationContext-security.xml

  <security:http auto-config="false" use-expressions="true" entry-point-ref="customAuthEntryPoint" >
    <security:form-login login-page='/login.jsp' default-target-url='/index.jsp'                             
                         authentication-failure-url="/login.jsp?error=true"
                         />    
    <security:access-denied-handler error-page="/errorPage.jsp"/> 
    <security:logout logout-success-url="/login.jsp?logout" />
...
    <bean id="customAuthEntryPoint" class="com.myapp.utils.CustomLoginUrlAuthenticationEntryPoint" scope="singleton">
        <constructor-arg value="/login.jsp" />
    </bean>
...
<bean id="requestCache" class="org.springframework.security.web.savedrequest.HttpSessionRequestCache">
    <property name="requestMatcher">
      <bean class="org.springframework.security.web.util.matcher.NegatedRequestMatcher">
        <constructor-arg>
          <bean class="org.springframework.security.web.util.matcher.MediaTypeRequestMatcher">
            <constructor-arg>
              <bean class="org.springframework.web.accept.HeaderContentNegotiationStrategy"/>
            </constructor-arg>
            <constructor-arg value="#{T(org.springframework.http.MediaType).APPLICATION_JSON}"/>
            <property name="useEquals" value="true"/>
          </bean>
        </constructor-arg>
      </bean>
    </property>
</bean>

在我的 JSP 中,添加一个全局 AJAX 错误处理程序,如下所示

  $( document ).ajaxError(function( event, jqxhr, settings, thrownError ) {
      if ( jqxhr.status === 403 ) {
          window.location = "login.jsp";
      } else {
          if(thrownError != null) {
              alert(thrownError);
          } else {
              alert("error");
          }
      }
  });

此外,从 JSP 页中的 AJAX 调用中删除现有的错误处理程序:

        var str = $("#viewForm").serialize();
        $.ajax({
            url: "get_mongoDB_doc_versions.do",
            type: "post",
            data: str,
            cache: false,
            async: false,
            dataType: "json",
            success: function(data) { ... },
//            error: function (jqXHR, textStatus, errorStr) {
//                 if(textStatus != null)
//                     alert(textStatus);
//                 else if(errorStr != null)
//                     alert(errorStr);
//                 else
//                     alert("error");
//            }
        });

我希望它能帮助其他人。

更新1我发现我需要将选项(always-use-default-target="true"(添加到表单登录配置中。这是必需的,因为在 AJAX 请求被重定向到登录页面(由于会话过期(后,Spring 会记住以前的 AJAX 请求并在登录后自动重定向到它。这会导致返回的 JSON 显示在浏览器页面上。当然,不是我想要的。

更新2不要使用 always-use-default-target="true" ,请使用@RobWinch阻止来自 requstCache 的 AJAX 请求的示例。这允许在登录后将普通链接重定向到其原始目标,但 AJAX 在登录后转到主页。

作为 ajax 的替代方案,正在开发一个新的 Fetch API,它允许手动重定向处理。您需要检查当前的浏览器支持是否足以满足您的需求。