Groovy can't compile the code from a solution

2020-08-05 11:02发布

Answer 1 to this StackOverflow post provides some Groovy code, but it doesn't compile (for me, Groovy Console Version 2.1.3, or in my Grails 2.2.3 app).

Can you please assist in letting me know what I need to change to make this code work? The error I get is:

 unexpected token: public at line 14, column: 52  (... which is the "public X509 Certificate[]")

For quick reference the code solution provided in Answer 1 is:

 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 )
 //================================

标签: grails groovy
1条回答
SAY GOODBYE
2楼-- · 2020-08-05 11:37

You probably have a newline between the public X509Certificate[] and getAcceptedIssuers() {null} as a result of pasting the code in.

Try removing the newline (and formatting the code to something moderately readable while you're at it) and the error should disappear.

查看更多
登录 后发表回答