jmeter3.0 源码分析之:对HTTPS协议的支持

本文通过分析jmeter 3.0的源码,探讨了其对HTTPS协议的支持方式,主要从HTTPHC4Impl类的sample方法入手,揭示了jmeter采用JSSE API,并通过LazySchemeSocketFactory和HC4TrustAllSSLSocketFactory类实现,默认信任所有证书的功能。

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

jmeter 3.0 的readme里有这样一句话:

Apache JMeter interfaces with the
Java Secure Socket Extension (JSSE) API to provide
- HTTPS support

于是查看了jmeter源码,想看看jmeter是如何对https做支持的。

首先从 org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl类入手:

重点关注sample方法,截取代码如下:

 public class HTTPHC4Impl extends HTTPHCAbstractImpl {

 @Override
    protected HTTPSampleResult sample(URL url, String method,
            boolean areFollowingRedirect, int frameDepth) {
            //...
            HttpClient httpClient = setupClient(url, res);          
            }

    private HttpClient setupClient(URL url, SampleResult res) {
        //...
        MeasuringConnectionManager connManager = new MeasuringConnectionManager(
                    createSchemeRegistry(), 
                    resolver, 
                    TIME_TO_LIVE,
                    VALIDITY_AFTER_INACTIVITY_TIMEOUT);

    }
    /**
     * Setup LazySchemeSocketFactory
     * @see "https://bz.apache.org/bugzilla/show_bug.cgi?id=58099"
     */
    private static SchemeRegistry createSchemeRegistry() {
        final SchemeRegistry registry = new SchemeRegistry();
        registry.register(
                new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); //$NON-NLS-1$
        registry.register(
                new Scheme("https", 443, new LazySchemeSocketFactory())); //$NON-NLS-1$
        return registry;
    }
 }

接着我们看看LazySchemeSocketFactory

public final class LazySchemeSocketFactory implements SchemeLayeredSocketFactory{

     /**
         * @throws SSLInitializationException
         */
        private static SchemeLayeredSocketFactory checkAndInit() throws SSLInitializationException {
            LOG.info("Setting up HTTPS TrustAll Socket Factory");
            try {
                return new HC4TrustAllSSLSocketFactory();
            } catch (GeneralSecurityException e) {
                LOG.warn("Failed to initialise HTTPS HC4TrustAllSSLSocketFactory", e);
                return SSLSocketFactory.getSocketFactory();
            }
        }
 }

这里又使用了HC4TrustAllSSLSocketFactory类:

public class HC4TrustAllSSLSocketFactory extends SSLSocketFactory {

     private static final TrustStrategy TRUSTALL = new TrustStrategy(){
        @Override
        public boolean isTrusted(X509Certificate[] chain, String authType) {
            return true;
        }
    };
    private javax.net.ssl.SSLSocketFactory factory;

    /**
     * Create an SSL factory which trusts all certificates and hosts.
     * {@link SSLSocketFactory#SSLSocketFactory(TrustStrategy, org.apache.http.conn.ssl.X509HostnameVerifier)} 
     * @throws GeneralSecurityException if there's a problem setting up the security
     */
    public HC4TrustAllSSLSocketFactory() throws GeneralSecurityException {
        this(new HttpSSLProtocolSocketFactory((JsseSSLManager)JsseSSLManager.getInstance(), JsseSSLManager.CPS));
    }

    /**
     * Create an SSL factory which trusts all certificates and hosts.
     * {@link SSLSocketFactory#SSLSocketFactory(TrustStrategy, org.apache.http.conn.ssl.X509HostnameVerifier)} 
     * @param factory javax.net.ssl.SSLSocketFactory 
     * @throws GeneralSecurityException if there's a problem setting up the security
     */
    protected HC4TrustAllSSLSocketFactory(javax.net.ssl.SSLSocketFactory factory) throws GeneralSecurityException {
        super(TRUSTALL, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        this.factory = factory;
    }
 }

到这里可以看出,jmeter对https的支持是默认信任所有证书的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值