Groovy无法从StackOverflow上给出的解决方案中编译代码 [英] Groovy can't compile the code from a solution given on StackOverflow
问题描述
回答1以这个StackOverflow文章提供了一些Groovy代码,但它不能编译(对于我来说,Grovy控制台版本2.1.3,或者在我的Grails 2.2.3应用程序中)。
你能否帮助我让我知道我需要改变这些代码的工作方式?我得到的错误是:
意外标记:public在第14行,第52列(...这是public X509证书[])
快速参考答案中提供的代码解决方案是:
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 [ ]作为TrustManager [],新的SecureRandom())
public void checkServerTrusted(X509Certificate [] certs,String authType){}
}] SSLSocketFactory(sslContext,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
def httpsScheme = new Scheme(https,sf,443)
http.client.connectionManager.schemeRegistry.register(httpsScheme)
// == ==============================
public X509Certificate []
和之间有一个换行符, getAcceptedIssuers(){null}
作为粘贴代码的结果。 尝试删除换行符(并将代码格式化为中等可读性而你在这里),错误应该消失。
Answer 1 to this StackOverflow post provides some Groovy code, but it doesn't compile (for me, Grovy 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 )
//================================
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.
这篇关于Groovy无法从StackOverflow上给出的解决方案中编译代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!