是否有更简单的方法来告诉HTTPBuilder忽略无效的证书?(Is there an easier

2019-06-26 12:37发布

每文档 ,你可以从浏览器中的证书手动完成对出口的一个相当笨重的过程,并得到其认可的本地。 有什么类似卷曲的--insecure切换,使这个现实?

Answer 1:

好消息大家好! :-)刚刚发现HttpBuilder的新版本(0.7.1)介绍的方法:

ignoreSSLIssues()

这解决了关于无效的SSL证书(当然你必须要知道,它也降低了安全性)的所有问题。

有关此方法的更多信息: https://github.com/jgritman/httpbuilder/wiki/SSL (在底部部分)



Answer 2:

找到一种方式,不涉及证书或httpbuilder黑客的进口

//== HTTPBUILDER IMPORTS
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.0-RC2' )
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
//== END HTTPBUILDER IMPORTS

import javax.net.ssl.X509TrustManager
import javax.net.ssl.SSLContext
import java.security.cert.X509Certificate
import javax.net.ssl.TrustManager
import java.security.SecureRandom
import org.apache.http.conn.ssl.SSLSocketFactory
import org.apache.http.conn.scheme.Scheme
import org.apache.http.conn.scheme.SchemeRegistry

def http = new HTTPBuilder( "https://your_unsecure_certificate_host" )

    //=== SSL UNSECURE CERTIFICATE ===
   def sslContext = SSLContext.getInstance("SSL")              
   sslContext.init(null, [ new X509TrustManager() {public X509Certificate[]   
   getAcceptedIssuers() {null }
   public void checkClientTrusted(X509Certificate[] certs, String authType) { }
   public void checkServerTrusted(X509Certificate[] certs, String authType) { }
   } ] as TrustManager[], new SecureRandom())
   def sf = new SSLSocketFactory(sslContext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
   def httpsScheme = new Scheme("https", sf, 443)
   http.client.connectionManager.schemeRegistry.register( httpsScheme )
   //================================

//do your http call with the http object
http.request( ....


文章来源: Is there an easier way to tell HTTPBuilder to ignore an invalid cert?