【问题标题】:Download file over HTTPS and SSO in groovy avoiding server certificate validation在 groovy 中通过 HTTPS 和 SSO 下载文件,避免服务器证书验证
【发布时间】:2016-02-09 22:41:21
【问题描述】:

我想通过使用单点登录 (SSO) 和 HTTPS (SSL) 的连接下载 groovy 中的文件,是否有一种简单的方法可以做到这一点。我不打算构建一个完整的应用程序,因此安全性不是那么重要。

def data =  new URL("https://server/context/servlet?param1=value1").getText()
print data

我目前使用 curl 进行下载,但理想情况下不必调用 curl。当前使用的调用如下。

curl --negotiate -u user:pass -L --insecure -o filename.txt  "https://server/context/servlet?param1=value1" 

我正在寻找的解决方案的两个关键点 - 它不涉及对 curl 进行系统调用 - 不包括手动设置证书。

会考虑图书馆。

【问题讨论】:

标签: ssl groovy https


【解决方案1】:

为避免 SSL PKIX 验证检查,在 Groovy 中,您可以像在 Java 中一样实现X509TrustManager

请注意,这会禁用验证服务器证书验证,因此存在安全风险:

import javax.net.ssl.*
    
// create a TrustManager to avoid PKIX path validation
def trustManager = [
  checkClientTrusted: { chain, authType ->  },
  checkServerTrusted: { chain, authType ->  },
  getAcceptedIssuers: { null }
] as X509TrustManager

// creat a sslCtx to use "lax" trustManager     
def context = SSLContext.getInstance("TLS")
context.init(null, [trustManager] as TrustManager[], null)
// set as default ssl context   
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory())

// finally you can connect to the server "insecurely"
def data =  new URL("https://server/context/servlet?param1=value1").getText()
print data

关于您的第二个问题,要提供像 curl 这样使用 --user 参数的基本身份验证,您可以使用 Authenticator 类为您的连接设置默认用户/密码:

Authenticator.setDefault (new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication ("user", "pass".toCharArray())
    }
})

请注意,在 Groovy 中使用某些库也可以通过其他方式执行此操作,但这是使用标准 Java 类的一种可能方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-19
    • 2014-09-16
    • 2016-05-24
    • 2015-11-01
    • 1970-01-01
    • 2021-12-16
    • 2018-03-15
    • 2021-03-08
    相关资源
    最近更新 更多