Fiddler 调试 HTTPS Java 应用程序

1、关闭自动抓取,只监听 8888 端口。

点击 Capturing

2、导出根证书文件。

Tool --> Options --> HTTPS --> Actions --> Export Root Certificate to Desktop

3、在 C 盘下创建 keystore 文件夹,将 Fiddler 证书拷贝到 keystore 目录下。

4、创建密钥库文件。

.cer file --> .jks file

cmd 输入命令:keytool -importcert -alias fiddler -file c:\keystore\FiddlerRoot.cer -keystore c:\keystore\fiddler_keystore.jks -storepass abc123456

是否信任此证书:是

说明:

(1)storepass 是密钥,可根据需要设置。

5、设置 https 代理服务器信息。

/**
 * 设置http代理
 * */
public void enableHttpProxy() {
    System.setProperty("https.proxyHost", "127.0.0.1");
    System.setProperty("https.proxyPort", "8888");
}

注意:

(1)https.proxyhost,是 https 不是 http

6、使用 RestTemplate 调用服务。

public class DemoMain {
    public static void main(String[] args) {

        enableHttpProxy(); // set system properties for http proxy

        RestTemplate restTemplate = new RestTemplate();
        String text = restTemplate.getForObject("https://wx.sanguosha.com/api/clock/do", String.class);
        System.out.println(text);
    }


    public static void enableHttpProxy() {
        System.setProperty("https.proxyHost", "127.0.0.1");
        System.setProperty("https.proxyPort", "8888");
    }
}

7、使用密钥库文件。

如果直接运行先前的 DemoMain 类,则在 SSL 握手过程中将抛出异常,由于无法找到验证证书。

sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

因此,我们需要使用以下JVM选项运行DemoMain类:

-Djavax.net.ssl.trustStore=c:/keystore/fiddler_keystore.jks -Djavax.net.ssl.trustStorePassword=abc123456

说明:

(1)这两个选项告诉 JVM 在哪里可以找到密钥库文件以及使用密钥库的相应密码。

(2)使用上述选项运行 DemoMain 之后,我们能够从 Fiddler UI 看到 https 请求及其响应。请求和响应都被解码,因此在开发过程中非常有帮助。

8、Fiddler 抓包查看。

作者:magic_kid_2010,如果觉得笔者的博客对您有所帮助,欢迎【犒赏

发布了82 篇原创文章 · 获赞 13 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/magic_kid_2010/article/details/101197813