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

本文详细介绍了在使用Amazon EC2运行Amazon S3时,将配置证书更改为默认EC2证书后遇到的SSL异常问题。通过分析Amazon S3源码,解释了为何直接更改S3Client实例会导致异常,以及如何通过正确调用AmazonHttpClient的disableStrictHostnameVerification()方法解决此问题。文章最后提供了实际操作步骤和代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用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.

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值