3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ちょっとしたことがGroovy「エラーの出るSSL証明書を無視してHTTPSアクセスをテストする」

Posted at

俺俺証明書だったり、適当に流用してきたSSL証明書で作ったテスト用サイトなんかへのHTTPSアクセスをテストしようとすると、SSL証明書のエラーが起きてテスト不可能です。

以下のようにすると無視できます。

ignoreSSLErr.groovy
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.6')

import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*

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 printlnErr = System.err.&println
def http = new HTTPBuilder( 'https://mySSLsite.hoge' )

/* 俺俺証明書だとかのSSL証明書のエラーを無視する */
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 )

/* リクエスト */
http.request( GET, JSON ) {
  uri.path = "/admin"
  response.success = {
    println "done 200. ${uri.path}"
  }
  response.failure = {res ->
    printlnErr "error ${res.statusLine.statusCode}. ${uri.path}"
  }
}

以下の記事をそのまま流用しています。

3
4
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?