What is the replacement for deprecated BasicAuthorizationInterceptor?

Viewed 4156

I am using a new BasicAuthorizationInterceptor to do the basic authentication in oauth2.0. I can't find a replacement for the deprecated BasicAuthorizationInterceptor. Please help me with it

5 Answers

Using BasicAuthenticationInterceptor worked for me.

From the BasicAuthorizationInterceptor documentation:

Deprecated as of 5.1.1, in favor of BasicAuthenticationInterceptor which reuses HttpHeaders.setBasicAuth(java.lang.String, java.lang.String), sharing its default charset ISO-8859-1 instead of UTF-8 as used here

This is how solved my problem:

  • in favor of BasicAuthenticationInterceptor which reuses HttpHeaders.setBasicAuth(java.lang.String, java.lang.String), sharing its default charset ISO-8859-1 instead of UTF-8 as used here 1

Using BasicAuthenticationInterceptor and also looking for the interface of ClientHttpRequestInterceptor

I am answering a bit late but most of the above answers say BasicAuthenticationInterceptor can be used instead of BasicAuthorizationInterceptor which is deprecated now. But if you see the implementation of both, you can find that in BasicAuthenticationInterceptor overriden intercept method as below:

  public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        HttpHeaders headers = request.getHeaders();
        if (!headers.containsKey("Authorization")) { // adds only if no Authorization header is absent
            headers.setBasicAuth(this.encodedCredentials);
        }

        return execution.execute(request, body);
    }

where as BasicAuthorizationInterceptor has below in the overriden intercept method:

    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        String token = Base64Utils.encodeToString((this.username + ":" + this.password).getBytes(StandardCharsets.UTF_8));
        request.getHeaders().add("Authorization", "Basic " + token); // no condition check
        return execution.execute(request, body);
    }

so as a quick summary, BasicAuthenticationInterceptor checks if an there is a Authorzation headers and adds only if not present but BasicAuthorizationInterceptor does not have that check.

So the basic solution is to create a Custom Interceptor which you might create referencing the BasicAuthorizationInterceptor class if you wanted to add multiple Authorization headers.

More info here

Related