Glide: Images url that include credentials in it won't be loaded

Created on 10 Nov 2016  路  5Comments  路  Source: bumptech/glide

Loading images which urls have the following pattern _https://username:[email protected]/somePath/someImage.png_ don't work.
Any suggestions? Workarounds?
Thanks in advance!


Glide Version: 3.7.0


Stack trace / LogCat:

D/GenericRequest: load failed
                                                                                java.io.IOException: Request failed 401: Unauthorized
                                                                                    at com.bumptech.glide.load.data.HttpUrlFetcher.loadDataWithRedirects(HttpUrlFetcher.java:90)
                                                                                    at com.bumptech.glide.load.data.HttpUrlFetcher.loadData(HttpUrlFetcher.java:44)
                                                                                    at com.bumptech.glide.load.data.HttpUrlFetcher.loadData(HttpUrlFetcher.java:20)
                                                                                    at com.bumptech.glide.load.model.ImageVideoModelLoader$ImageVideoFetcher.loadData(ImageVideoModelLoader.java:70)
                                                                                    at com.bumptech.glide.load.model.ImageVideoModelLoader$ImageVideoFetcher.loadData(ImageVideoModelLoader.java:53)
                                                                                    at com.bumptech.glide.load.engine.DecodeJob.decodeSource(DecodeJob.java:170)
                                                                                    at com.bumptech.glide.load.engine.DecodeJob.decodeFromSource(DecodeJob.java:128)
                                                                                    at com.bumptech.glide.load.engine.EngineRunnable.decodeFromSource(EngineRunnable.java:122)
                                                                                    at com.bumptech.glide.load.engine.EngineRunnable.decode(EngineRunnable.java:101)
                                                                                    at com.bumptech.glide.load.engine.EngineRunnable.run(EngineRunnable.java:58)
                                                                                    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
                                                                                    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                                    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                                    at java.lang.Thread.run(Thread.java:818)
                                                                                    at com.bumptech.glide.load.engine.executor.FifoPriorityThreadPoolExecutor$DefaultThreadFactory$1.run(FifoPriorityThreadPoolExecutor.java:118)
enhancement question wontfix

Most helpful comment

Based on http://stackoverflow.com/a/5137446/253468 I think this could be a workaround/solution (untested):

LazyHeaders auth = new LazyHeaders.Builder() // can be cached in a field and reused
        .addHeader("Authorization", new BasicAuthorization(username, password))
        .build();
Glide
        .with(context)
        .load(new GlideUrl(url, auth)) // GlideUrl is created anyway so there's no extra objects allocated
        .into(imageView);
}
public class BasicAuthorization implements LazyHeaderFactory {
    private final String username;
    private final String password;

    public BasicAuthorization(String username, String password) {
        this.username = username;
        this.password = password;
    }

    @Override public String buildHeader() {
        return "Basic " + Base64.encodeToString((username + ":" + password).getBytes(), Base64.NO_WRAP);
    }
}

All 5 comments

Based on http://stackoverflow.com/a/5137446/253468 I think this could be a workaround/solution (untested):

LazyHeaders auth = new LazyHeaders.Builder() // can be cached in a field and reused
        .addHeader("Authorization", new BasicAuthorization(username, password))
        .build();
Glide
        .with(context)
        .load(new GlideUrl(url, auth)) // GlideUrl is created anyway so there's no extra objects allocated
        .into(imageView);
}
public class BasicAuthorization implements LazyHeaderFactory {
    private final String username;
    private final String password;

    public BasicAuthorization(String username, String password) {
        this.username = username;
        this.password = password;
    }

    @Override public String buildHeader() {
        return "Basic " + Base64.encodeToString((username + ":" + password).getBytes(), Base64.NO_WRAP);
    }
}

Hi @TWiStErRob! Thank you very much! It worked! Cheers!

It's also possible that OkHttp handles this automatically, so using that integration library may also work.

Edit: no, it won't, however the above GlideUrl workaround works with any networking layer that supports headers.

This could be an enhancement in Glide, but based on https://tools.ietf.org/html/rfc7230#appendix-A.2 it's discouraged:

Userinfo (i.e., username and password) are now disallowed in HTTP and HTTPS URIs, because of security issues related to their transmission on the wire. (Section 2.7.1)

Use the above workaround if you need to authenticate.

@TWiStErRob tanks so much!!! Solution worked!!!

Was this page helpful?
0 / 5 - 0 ratings