grails - 使用 HttpBuilder 通过 SSL 上传文件

标签 grails ssl groovy https httpbuilder

我正在尝试使用 HttpBuilder 将文件上传到通过 SSL 运行的 Web 服务。我是从 Gradle 构建文件中执行此操作的,但我认为这并不重要。 Web 服务是 Grails 应用程序的一部分,但同样,不要认为这很重要。

当您告诉它在 https 上运行时,我的 grails 应用程序使用 grails 生成的证书在本地通过 SSL 运行。我在本地运行的代码如下:

def http = new groovyx.net.http.HTTPBuilder( 'https://localhost:8443/' )

//=== 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)
def httpsScheme = new Scheme("https", sf, 8443)
http.client.connectionManager.schemeRegistry.register( httpsScheme )

http.request( groovyx.net.http.Method.POST, groovyx.net.http.ContentType.JSON ) { req ->
    uri.path = '/a/admin/runtime/upload'
    uri.query = [ username: "username", password: "password", version: version]
    requestContentType = 'multipart/form-data'
    org.apache.http.entity.mime.MultipartEntity entity = new org.apache.http.entity.mime.MultipartEntity()
    def file = new File("file.zip")
    entity.addPart("runtimeFile", new org.apache.http.entity.mime.content.ByteArrayBody(file.getBytes(), 'file.zip'))
    req.entity = entity
}

顶部的所有垃圾都是我发现的一些代码,这些代码允许 HttpBuilder 使用自签名证书。这实际上是在本地工作。 HttpBuilder 文档说大多数时候,SSL 应该“正常工作”。因此,我使用合法购买的证书通过 SSL 执行此操作的代码如下:

def http = new groovyx.net.http.HTTPBuilder( 'https://www.realserver.com/' )

http.request( groovyx.net.http.Method.POST, groovyx.net.http.ContentType.JSON ) { req ->
    uri.path = '/a/admin/runtime/upload'
    uri.query = [ username: "username", password: "password" ]
    requestContentType = 'multipart/form-data'
    org.apache.http.entity.mime.MultipartEntity entity = new org.apache.http.entity.mime.MultipartEntity()
    def file = new File("file.zip")
    entity.addPart("runtimeFile", new org.apache.http.entity.mime.content.ByteArrayBody(file.getBytes(), 'file.zip'))
    req.entity = entity
}

当我运行它时,出现以下错误:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

如有任何提示或建议,我们将不胜感激。

最佳答案

这可能是因为该服务正在使用 HTTPS 的自签名证书。要正确地与之交互,您应该使用 keytool 将其导入 JRE 的 cacerts 文件。示例命令如下所示:

keytool -import -alias serviceCertificate -file ServiceSelfSignedCertificate.cer -keystore {path/to/jre/lib/security/cacerts}

关于grails - 使用 HttpBuilder 通过 SSL 上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21300035/

相关文章:

grails - 使属性在GORM中不可变

java - 在我的 OSGi 环境中安装新版本的 Groovy 会使我的包导入它,尽管它不应该

grails - 如何重用 Grails 应用程序的域类?

grails - 如何压缩 Grails Controller 的输出?

file - Grails-通过附加程序登录文件

php - 如何强制 Laravel 生成 https 链接?

具有自定义用户名密码身份验证问题的 WCF 服务

node.js - 如何使用 nodejs 禁用 tls 1.0 并仅使用 tls 1.1

gradle - 基于抽象类在Gradle中分离集成测试与单元测试

java - 为什么 com.ibm.jms.JMSTextMessage 类型的变量会打印截断的内容和省略号 (...)?