How to prevent Basic Authentication form to popup

眉间皱痕 提交于 2019-12-08 01:29:56

问题


I have a application in Java, JSF that uses javascript to connect to a website that needs Basic authentication.

The thing I want to accomplice is the exact same thing that happens when I type in username and password in the popupform.

I have tried many different ways that I have seen on the topic, but none of them works. The strange thing is that the ajax calls return a respone, but then I get the windows security popup anyway. Do I need to cache it someway? For example the both of the codes below I dont get to work. The one below uses the base64

$.ajax(
                {
                  'password' : password,
                  'username' : username,
                  'url'      : url,
                  'type'     : 'GET',
                  'success'  : function(){ alert("success");  },
                  'error'    : function(err){ alert('Bad Login Details' + err);},
                }
              );

$.ajax({
            url : url,
            method : 'GET',
            beforeSend : function(req) {
                req.setRequestHeader('Authorization', auth);
            },
            error : function(xhr, ajaxOptions, thrownError) {

                alert('Invalid username or password. Please try again. thrownError:' + thrownError + 'xhr:' + xhr + 'ajaxOptions:'+ajaxOptions);

            },
            sucess: function(result) {
                alert('done');
            }
        });

回答1:


You can encode basic-auth credentials in the URL in the following format:

https://username:password@www.example.com/abc

So adapt your ajax url the same way.




回答2:


If you have control of the server side code, you could roll your own Authenticate header to prevent the browser from popping up the standard Basic Challenge. For example, if you header is:

WWW-Authenticate: Basic realm="realm here"

Then the browser will pop up the challenge. But if you header is:

WWW-Authenticate: my-basic realm="insert realm"

Then the browser will not pop up a 401 challenge.



来源:https://stackoverflow.com/questions/14567991/how-to-prevent-basic-authentication-form-to-popup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!