Unable to execute HTTP request: hostname in certificate didn't match

在使用amazonS3的时候,当在 amazon ec2 上面运行 S3的时候,把配置的证书 修改为

使用默认的 amazon ec2的证书的时候,出现了一下异常

com.amazonaws.http.AmazonHttpClient executeHelper
INFO: Unable to execute HTTP request: hostname in certificate didn't match: <com.example.com.s3.amazonaws.com> != <*.s3.amazonaws.com> OR <*.s3.amazonaws.com> OR <s3.amazonaws.com>
javax.net.ssl.SSLException: hostname in certificate didn't match: <com.example..com.s3.amazonaws.com> != <*.s3.amazonaws.com> OR <*.s3.amazonaws.com> OR <s3.amazonaws.com>

从amazon 文档来看 只需要在构建 S3Client的时候,从

 ClientConfiguration clientConfiguration = new ClientConfiguration();

AWSCredentials credentials = new ClasspathPropertiesFileCredentialsProvider().getCredentials();

AmazonS3Client s3Client = new AmazonS3Client(credentials,clientConfiguration);

修改为  AmazonS3Client s3Client = new AmazonS3Client();

s3Client.setConfiguration(clientConfiguration);

就应该可以啦;

但是为什么不可以呢?在amaoznS3 的源码里面发现 原来

可此时 为什么会出上面的错误呢?

原来 在 AmazonS3Client 的构造函数中,会利用传入的ClientConfiguration或者系统默认的ClientConfiguration创建AmazonHttpClient,并且设置AmazonHttpClient为disableStrictHostnameVerification(),而咱们显示设置clientConfiguration的时候

会重新创建AmazonHttpClient,并且没有显示的设置

AmazonHttpClient为disableStrictHostnameVerification(),

而设置disableStrictHostnameVerification() 没有提供外部函数, 只有在构造函数里面才能调用;

所以我们需要这样调用

credentials=new InstanceProfileCredentialsProvider().getCredentials();// 得到当前ec2 instance 的证书
        s3Client = new AmazonS3Client(credentials, clientConfiguration);

disableStrictHostnameVerification()的作用就是

// Because of S3's virtual host style addressing, we need to change the
        // default, strict hostname verification to be more lenient.

猜你喜欢

转载自yjc0407.iteye.com/blog/1884103