有没有更简单的方法告诉HTTPBuilder忽略无效的证书?

时间:2012-07-24 19:32:16

标签: groovy httpbuilder

根据docs,您可以通过手动从浏览器导出证书并在本地识别它来进行相当笨重的过程。是否有类似于curl的--insecure切换以使其变得实用?

2 个答案:

答案 0 :(得分:28)

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

ignoreSSLIssues()

这解决了有关无效SSL证书的所有问题(当然,您必须意识到它也会降低安全性)。

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

答案 1 :(得分:5)

找到一种不涉及导入证书或httpbuilder hacks的方法

//== 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( ....