question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Authenticator.setDefault() with Fresco requests

See original GitHub issue

Hi I’m using Fresco to show images from a password-protected webservice. I’m setting the default authenticator for my http/https requests in my app’s onCreate like this:

        Authenticator.setDefault(new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(
                        userWebService, pwdWebService.toCharArray());
            }
        });

But everytime I call

                SimpleDraweeView image = (SimpleDraweeView) findViewById(R.id.imageView_product);
                GenericDraweeHierarchy h = image.getHierarchy();
                h.setActualImageScaleType(ScalingUtils.ScaleType.CENTER_INSIDE);
                image.setImageURI(uri);

the request is fired twice: the first fails the authentication (response 401) while the second authenticate correctly (response 200). I’m not 100% sure but I actually feel like it’s a Fresco issue since the code is really simple. Is anyone able to test this?

Thanks, Kind Regards.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Doomkyncommented, Oct 7, 2016

UPDATE: Actually seems like a bug of the native android network layer (HttpURLConnection). Previously I was doing this to initialize Fresco:

Authenticator.setDefault(new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(
                        userWebService, pwdWebService.toCharArray());
            }
        });

Fresco.initialize(this);

In this case, the agency that provide me the WebService form where I load my images told me they always see 2 requests for every image, the first without authentication header and the second correctly authenticated.

Then, thanks to @massimocarli’s post , I tried using OkHttp3 as network layer, implementing it like this:

String credentials = userWebService + ":" + pwdWebService;
        final String basic =
                "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
        final OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder()
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        final Request.Builder requestBuilder = chain.request().newBuilder();
                        requestBuilder.addHeader("Authorization", basic);
                        return chain.proceed(requestBuilder.build());
                    }
                });
        final ImagePipelineConfig imagePipelineConfig = OkHttpImagePipelineConfigFactory.newBuilder(
                getApplicationContext(),
                okHttpClientBuilder.build())
                .build();

        Fresco.initialize(this, imagePipelineConfig);

And now it seems to work fine: only one authenticated request is fired.

I’m not closing this, I actually wait for a mod to close this so if there is a more technical consideration to do, it can be done.

Kind regards.

0reactions
massimocarlicommented, Oct 7, 2016

Great @Doomkyn, that is actually the solution that we usually suggest for this 😃

Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Authenticator (Java Platform SE 7 ) - Oracle Help Center
All methods that request authentication have a default implementation that fails. Since: 1.2; See Also: setDefault(java.net.Authenticator) , ...
Read more >
How to handle multiple Authenticator - Stack Overflow
Do I need to Authenticator.setDefault for each request ? This may not work if there are concurrent connection with mixed webservices... android.
Read more >
Questions and Answers Authentication - TechDocs
The Question and Answer (QnA) authentication mechanism can either be used as a secondary authentication method for CA Auth ID Information, ...
Read more >
Sentinels Rolling Archive User Access, Operations ...
the management of user requests and products publication to end users is managed by one DHuS instance called “Front-End”.
Read more >
Overriding current java.net.Authenticator for specific requests
Authenticator.setDefault() if I want it to send its HTTP/HTTPS requests through a proxy that requires a username and a password.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found