Android-Universal-Image-Loader icon indicating copy to clipboard operation
Android-Universal-Image-Loader copied to clipboard

Universal Image Loader and HTTPS (Apache HTTP Component)

Open t0mm13b opened this issue 13 years ago • 6 comments

Have noticed, since I use my custom HTTPS SSL Socket factory in my app, using in conjunction with UniversalImageLoader, the loading of the images failed.

t0mm13b avatar Apr 08 '13 12:04 t0mm13b

The only thing that I could do to my custom SSL Socket Factory was the following -

  • implement a getter to return back your SSLContext
  • implement a getter to return back your custom host verifier

It must be noted the following:

  • MyCustomSSLSocketHandler is derived from Apache's HTTP Components library (4.1.x) and uses its own host verifier and keystore trust manager, i.e.
public class MyCustomSSLSocketHandler extends SSLSocketFactory implements SchemeSocketFactory{
}

Then apply to the following sample of code taken from issue 188, as shown:
```java
public class SecureImageDownloader extends BaseImageDownloader{
    private static final String TAG = SecureImageDownloader.class.getName();    

    public SecureImageDownloader(Context context){
        super(context);
    }
    public SecureImageDownloader(Context context, int connectTimeout, int readTimeout) {
        super(context, connectTimeout, readTimeout);
    }


    @Override
    protected InputStream getStreamFromNetwork(String imageUri, Object extra) throws IOException {
        URL url = null;
        try {
            url = new URL(imageUri);
        } catch (MalformedURLException e) {
            Log.e(TAG, e.getMessage(), e);
        }

        HttpURLConnection http = null;

        if (Scheme.ofUri(imageUri) == Scheme.HTTPS) {
            HttpsURLConnection.setDefaultSSLSocketFactory(MyCustomSSLSocketHandler.getSSLContext().getSocketFactory());
            HttpsURLConnection.setDefaultHostnameVerifier(MyCustomSSLSocketHandler.getCustomHostnameVerifier());
            HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
            http = https;
            http.connect();
        } else {
            http = (HttpURLConnection) url.openConnection();
        }
        http.setConnectTimeout(connectTimeout);
        http.setReadTimeout(readTimeout);
       return new FlushedInputStream(new BufferedInputStream(http.getInputStream()));
    }
}

There was a way to do it in a manner to avoid the dreaded cast exception between javax.net.ssl.SSLSocketFactory and Apache's own HTTP Component's SSLSocket factory

What I did initially was this: https.setDefaultSSLSocketFactory(MyCustomSSLSocketHandler.getSocketFactory()); which resulted in a cast exception. The above code is stated to work by using the HttpsURLConnection's setDefaultSSLSocketFactory instead. - FYI for others who may have had difficulty in doing this.

I will verify shortly to ensure that the trust manager is indeed being used, to mitigate MITM attacks....

t0mm13b avatar Apr 08 '13 18:04 t0mm13b

Regarding my previous comment - I can indeed confirm that it is using my own custom keystore trust manager and host name verifier.

Thanks! :)

t0mm13b avatar Apr 08 '13 18:04 t0mm13b

I read all that and didn't understand: do you still have any problem or question?

nostra13 avatar Apr 10 '13 18:04 nostra13

The problem was resolved. I thought I'd leave it here for others in case they encounter similar situation with Apache's HTTP Component :)

t0mm13b avatar Apr 10 '13 18:04 t0mm13b

Hi nostra, I have been using the universal image loader for my project. I am using ImageLoader object instance.

ImageLoader imageLoader = ImageLoader.getInstance();
                @Override
                public Object instantiateItem(View view, int position) {
                    final FrameLayout imageLayout = (FrameLayout) inflater.inflate(R.layout.item_gallery_image, null);
                    final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
                    final ProgressBar loading = (ProgressBar) imageLayout.findViewById(R.id.loading);

                    selectedPosition=position;
                    Log.d("selected item", String.valueOf(selectedPosition));

                    DisplayImageOptions options = new DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.ic_launcher)
                        .cacheInMemory()
                        .cacheOnDisc()
                    //  .decodingType(DecodingType.MEMORY_SAVING)
                        .build();
                    imageLoader.displayImage(images[position], imageView, options, new SimpleImageLoadingListener() {

                        @Override
                        public void onLoadingStarted(String imageUri, View view) {
                            // TODO Auto-generated method stub
                            super.onLoadingStarted(imageUri, view);
                            loading.setVisibility(View.VISIBLE);
                        }


                        @Override
                        public void onLoadingFailed(
                                String imageUri,
                                View view,
                                com.nostra13.universalimageloader.core.assist.FailReason failReason) {
                            // TODO Auto-generated method stub
                            super.onLoadingFailed(imageUri, view, failReason);
                            loading.setVisibility(View.GONE);
                            imageView.setImageResource(android.R.drawable.ic_delete);


                            switch (failReason.getType()) {
                            case OUT_OF_MEMORY:
                                imageLoader.clearMemoryCache();
                                break;
                            case NETWORK_DENIED:
                                Toast.makeText(getApplicationContext(), "Unable to download image. Network denied", Toast.LENGTH_LONG).show();
                                break;
                        }
                        }


                        @Override
                        public void onLoadingComplete(String imageUri,
                                View view, Bitmap loadedImage) {
                            // TODO Auto-generated method stub
                            super.onLoadingComplete(imageUri, view, loadedImage);
                            loading.setVisibility(View.GONE);
                        }

                    });

Can you guide me to change the above code for HTTPS.

tapas4git avatar Apr 29 '14 11:04 tapas4git

@tapas4git did you find the answer for your question ? I have the same problem. My code is like;

Map<String,String> authHeaders = new HashMap<>(); authHeaders.put("token", LocalStorage.getUserToken(ctx));

    DisplayImageOptions defaults = new DisplayImageOptions.Builder()
            .displayer(new FadeInBitmapDisplayer(300, true, true, false))
            .showImageOnFail(R.drawable.ic_image_fail)
            .showImageForEmptyUri(R.drawable.ic_image_fail)
            .cacheOnDisk(true)
            .cacheInMemory(true)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .extraForDownloader(authHeaders)
            .build();
    ImageLoaderConfiguration.Builder configsBuilder = new ImageLoaderConfiguration.Builder(ctx)

            .imageDownloader(new AuthImageDownloader(ctx))
            .defaultDisplayImageOptions(defaults);
    ImageLoader.getInstance().init(configsBuilder.build());

How can make it https ?

mehmeth avatar Mar 19 '18 17:03 mehmeth