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

后端 未结 11 1136
隐瞒了意图╮
隐瞒了意图╮ 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 12:53

    I was facing this issue recently, too. Since you can't change the browser's default behavior of showing the popup in case of a 401 (basic or digest authentication), there are two ways to fix this:

    • Change the server response to not return a 401. Return a 200 code instead and handle this in your jQuery client.
    • Change the method that you're using for authorization to a custom value in your header. Browsers will display the popup for Basic and Digest. You have to change this on both the client and the server.

      headers : {
        "Authorization" : "BasicCustom"
      }
      

    Please also take a look at this for an example of using jQuery with Basic Auth.

    0 讨论(0)
  • 2020-11-27 12:55

    Alternatively, if you can customize your server response, you could return a 403 Forbidden.

    The browser will not open the authentication popup and the jquery callback will be called.

    0 讨论(0)
  • 2020-11-27 12:57

    Return a generic 400 status code, and then process that client-side.

    Or you can keep the 401, and not return the WWW-Authenticate header, which is really what the browser is responding to with the authentication popup. If the WWW-Authenticate header is missing, then the browser won't prompt for credentials.

    0 讨论(0)
  • 2020-11-27 12:59

    From back side with Spring Boot I've used custom BasicAuthenticationEntryPoint:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().authorizeRequests()
                ...
                .antMatchers(PUBLIC_AUTH).permitAll()
                .and().httpBasic()
    //    https://www.baeldung.com/spring-security-basic-authentication
                .authenticationEntryPoint(authBasicAuthenticationEntryPoint())
                ...
    
    @Bean
    public BasicAuthenticationEntryPoint authBasicAuthenticationEntryPoint() {
        return new BasicAuthenticationEntryPoint() {
            {
                setRealmName("pirsApp");
            }
    
            @Override
            public void commence
                    (HttpServletRequest request, HttpServletResponse response, AuthenticationException authEx)
                    throws IOException, ServletException {
                if (request.getRequestURI().equals(PUBLIC_AUTH)) {
                    response.sendError(HttpStatus.PRECONDITION_FAILED.value(), "Wrong credentials");
                } else {
                    super.commence(request, response, authEx);
                }
            }
        };
    }
    
    0 讨论(0)
  • 2020-11-27 13:00

    Use X-Requested-With: XMLHttpRequest with your request header. So the response header will not contain WWW-Authenticate:Basic.

    beforeSend: function (xhr) {
                        xhr.setRequestHeader('Authorization', ("Basic "
                            .concat(btoa(key))));
                        xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
                    },
    
    0 讨论(0)
  • 2020-11-27 13:02

    Make an /login url, than accept "user" and "password" parameters via GET and don't require basic auth. Here, use php, node, java, whatever and parse your passwd file and match parameters (user/pass) against it. If there is a match then redirect to http://user:pass@domain.com/ (this will set credential on your browser) if not, send 401 response (without WWW-Authenticate header).

    0 讨论(0)
提交回复
热议问题