How to prevent browser to invoke basic auth popup and handle 401 error using Jquery?

后端 未结 11 1137
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 12:18

I need to send authorization request using basic auth. I have successfully implemented this using jquery. However when I get 401 error basic auth browser popup is opened and

相关标签:
11条回答
  • 2020-11-27 13:03

    If WWW-Authenticate header is removed, then you wont get the caching of credentials and wont get back the Authorization header in request. That means now you will have to enter the credentials for every new request you generate.

    0 讨论(0)
  • 2020-11-27 13:05

    In Safari, you can use synchronous requests to avoid the browser to display the popup. Of course, synchronous requests should only be used in this case to check user credentials... You can use a such request before sending the actual request which may cause a bad user experience if the content (sent or received) is quite heavy.

        var xmlhttp=new XMLHttpRequest;
        xmlhttp.withCredentials=true;
        xmlhttp.open("POST",<YOUR UR>,false,username,password);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
    
    0 讨论(0)
  • 2020-11-27 13:12

    If you're using an IIS Server, you could setup IIS URL Rewriting (v2) to rewrite the WWW-Authentication header to None on the requested URL.

    Guide here.

    The value you want to change is response_www_authenticate.

    If you need more info, add a comment and I'll post the web.config file.

    0 讨论(0)
  • 2020-11-27 13:14

    You can suppress basic auth popup with request url looking like this:

    https://username:password@example.com/admin/...
    

    If you get 401 error (wrong username or password) it will be correctly handled with jquery error callback. It can cause some security issues (in case of http protocol instead of https), but it's works.

    UPD: This solution support will be removed in Chrome 59

    0 讨论(0)
  • 2020-11-27 13:15

    As others have pointed out, the only way to change the browser's behavior is to make sure the response either does not contain a 401 status code or if it does, not include the WWW-Authenticate: Basic header. Since changing the status code is not very semantic and undesirable, a good approach is to remove the WWW-Authenticate header. If you can't or don't want to modify your web server application, you can always serve or proxy it through Apache (if you are not using Apache already).

    Here is a configuration for Apache to rewrite the response to remove the WWW-Authenticate header IFF the request contains contains the header X-Requested-With: XMLHttpRequest (which is set by default by major Javascript frameworks such as JQuery/AngularJS, etc...) AND the response contains the header WWW-Authenticate: Basic.

    Tested on Apache 2.4 (not sure if it works with 2.2). This relies on the mod_headers module being installed. (On Debian/Ubuntu, sudo a2enmod headers and restart Apache)

        <Location />
                # Make sure that if it is an XHR request,
                # we don't send back basic authentication header.
                # This is to prevent the browser from displaying a basic auth login dialog.
                Header unset WWW-Authenticate "expr=req('X-Requested-With') == 'XMLHttpRequest' && resp('WWW-Authenticate') =~ /^Basic/"
        </Location>   
    
    0 讨论(0)
提交回复
热议问题