在Grails中使用TrustManager [] trustAllCerts = new TrustManager []

时间:2016-03-29 06:00:07

标签: groovy

如何在grails中使用以下代码 -

TrustManager[] trustAllCerts = new TrustManager[]{
                new X509TrustManager() {
                    public X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }

                    public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
                        return;
                    }

                    public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
                        return;
                    }
                }
        };

当我在JAVA项目中运行相同的代码时,上面的代码工作正常,但Grails没有编译代码并给出错误 - 第一行上的数组构造函数调用没有表达式

1 个答案:

答案 0 :(得分:7)

以下代码将起作用:

import javax.net.ssl.X509TrustManager
import javax.net.ssl.TrustManager
import java.security.cert.X509Certificate
import java.security.cert.CertificateException

def trustAllCerts = [
    new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return null
        }

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

        public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
        }
    }
] as TrustManager[] 

查看this问题。