开发者

Errors with SSL validation in Grails

开发者 https://www.devze.com 2023-03-14 21:43 出处:网络
I want to make a post to an url with SSL validation in Grails. I\'ve imported the certificates, and I\'ve generated the jks files.

I want to make a post to an url with SSL validation in Grails. I've imported the certificates, and I've generated the jks files. But it seems Grails does not found those jks files. Below is the code I'm executing (it's in a Grails Service Class):

def certificate = "mycert.jks"

def http = new HTTPBuilder(url)

def keyStore = KeyStore.getInstance(KeyStore.defaultType)
getClass().getResource(certificate).withInputStream {
    keyStore.load(it, "test1234".toCharArray())
}

http.client.connectionManager.schemeRegistry.register(new Scheme("https", new SSLSocketFactory(keyStore), 443))

The error is "Cannot invoke method withInputStream() on开发者_开发技巧 null object" so the method getResource() is not working, and returns null.

I saw that this code, in a functionalTest Class, works fine.

Anyone knows how can I solve this problem?

Thanks!!!!!!!


The problem is that non-source files from grails-app/services aren't copied into the classpath. This is the case for src/java though, but not src/groovy. It's also the case for grails-app/conf, but that's the only grails-app folder that this happens for, since it's the configuration file folder.

So your best bet is to move the file to grails-app/conf and use

Thread.currentThread().contextClassLoader.getResource(certificate).withInputStream {
    keyStore.load(it, "test1234".toCharArray())
}
0

精彩评论

暂无评论...
验证码 换一张
取 消