grails - RestClientBuilder

2019-04-13 23:45发布

问题:

I am using the current version of rest client builder plugin. I tested out the uri via curl:

curl --user username:password https://localhost:8085/rest/api/latest/plan.json?os_authType=basic

I get the expected json in return. When I try to translate this to grails using the plugin like this:

RestBuilder rb = new RestBuilder()
    def response = rb.get("https://localhost:8085/rest/api/latest/plan.json?os_authType=basic"){     
            auth 'username', 'password'
        }

response.json instanceof JSONObject

I get this error:

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

Why does it work in curl and not with the plugin? How do I get this to work?

Thanks!

回答1:

You need to add the root certificate to the store of the trusted ones.

http://docs.oracle.com/javase/tutorial/security/toolsign/rstep2.html



回答2:

You can just disable SSL check for RestBuilder.

See an example of code:

static Scheme disableSSLCheck() {

        def sslContext = SSLContext.getInstance("SSL")
        sslContext.init(null, [new X509TrustManager() {
            public void checkClientTrusted(X509Certificate[] certs, String authType) {}

            public void checkServerTrusted(X509Certificate[] certs, String authType) {}

            @Override
            X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0]
            }
        }] as TrustManager[], new SecureRandom())
        def sf = new SSLSocketFactory(sslContext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
        def httpsScheme = new Scheme("https", sf, 443)
        httpsScheme

    }

And register this Scheme to the RestClient:

Scheme httpsScheme = disableSSLCheck()
restClient.client.connectionManager.schemeRegistry.register(httpsScheme)


回答3:

Mb too late but have a look here. https://gist.github.com/thomastaylor312/80fcb016020e4115aa64320b98fb0017

I do have it as separate method in my Integration test

 def static disableSSLCheck() {
    def nullTrustManager = [
            checkClientTrusted: { chain, authType -> },
            checkServerTrusted: { chain, authType -> },
            getAcceptedIssuers: { null }
    ]

    def nullHostnameVerifier = [
            verify: { hostname, session -> true }
    ]

    SSLContext sc = SSLContext.getInstance("SSL")
    sc.init(null, [nullTrustManager as X509TrustManager] as TrustManager[], null)
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory())
    HttpsURLConnection.setDefaultHostnameVerifier(nullHostnameVerifier as HostnameVerifier)
}

And then just

void "test authentication"(){
    given:
    String url = "j_spring_security_check"
    MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>()
    form.add("grant_type", "password")
    form.add("j_username", "vadim@ondeviceresearch.com")
    form.add("j_password", "notSecure")
    form.add("_spring_security_remember_me", "true")
    //TODO SET username and pass
    //todo get token back
    disableSSLCheck()
    when:
    RestResponse response = rest.post(host + url){
        accept("application/json")
        contentType("application/x-www-form-urlencoded")
        body(form)
    }
    response
    then:
    response.status == 200
}


标签: rest grails curl