【问题标题】:How to do a POST call to secure site with restlet that is working fine with curl POST如何使用可与 curl POST 正常工作的 restlet 对安全站点进行 POST 调用
【发布时间】:2015-04-21 07:27:58
【问题描述】:

我的 curl 命令运行良好:

curl -u "myusername":"mypassword" -X POST -H "Content-Type: application/json" -d '{
            "data": [
                {
                    "val": 867.8,
                    "date": "2014-06-18T09:56:21+00:00"
                },
                {
                    "val": 98.5432,
                    "date": "2014-06-18T09:58:21+00:00"
                }
            ],
            "user_id": "786733",
            "pulse_id": "1",
          }' https://www.myweb.com/Test/requests

请注意,连接是 https,即它使用 SSL,我没有提到要给出任何 certificate/keystore/truststore

但是无论我是否添加信任代码,我的 java 代码都无法正常工作。

如果我在本地提供 URL,即 localhost,我的代码可以正常工作。

我正在使用 RESTLET POST 调用。

例如:http://localhost:8085/Test/requests

但如果我将 URL 替换为远程主机的 https,则不起作用。

例如:https://www.myweb.com/Test/requests

以下只是提到远程URL:

Caused by: 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
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1884)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:276)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:270)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1439)
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:209)
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:878)
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:814)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1016)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
    at org.restlet.engine.connector.HttpUrlConnectionCall.sendRequest(HttpUrlConnectionCall.java:356)
    at org.restlet.engine.adapter.ClientAdapter.commit(ClientAdapter.java:105)
    at org.restlet.engine.adapter.HttpClientHelper.handle(HttpClientHelper.java:119)
    at org.restlet.Client.handle(Client.java:153)
    at org.restlet.routing.Filter.doHandle(Filter.java:150)
    at org.restlet.routing.Filter.handle(Filter.java:197)
    at org.restlet.resource.ClientResource.handle(ClientResource.java:1092)
    at org.restlet.resource.ClientResource.handleOutbound(ClientResource.java:1176)
    at org.restlet.resource.ClientResource.handle(ClientResource.java:1047)
    ... 6 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:385)
    at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
    at sun.security.validator.Validator.validate(Validator.java:260)
    at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:326)
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:231)
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:126)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1421)
    ... 25 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:196)
    at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:268)
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:380)

代码:

客户端:

String json = gson.toJson(obj); 
ClientResource clientResource = 
new ClientResource("https://www.myweb.com/Test/requests");
StringRepresentation rep = new StringRepresentation(json);
clientResource.post(rep.getText());

服务器端:

Main.java -

Component component = new Component();
component.getServers().add(Protocol.HTTPS, port);           
component.getDefaultHost().attach("/www.myweb.com"),new UserApplication());
component.start();  

UserApplication.java -

@Override
public Restlet createInboundRoot() {    
    String routerName = "/Test";
    String aPIName = "/requests";
    Router rootRouter2 = new Router(getContext());  
    Router getAdapterRouter = new Router(getContext());
    getEnerNocAdapterRouter.attach(aPIName, PersistentData.class);
    rootRouter2.attach(routerName, getAdapterRouter);
    return rootRouter2;
}

PersistentData.java-

@Post
public void receiveData(Representation rep) throws ResourceException{
    LOGGER.info("Server received Data");
    try {
        LOGGER.info("Server Data: "+rep.getText().toString());
    } catch (IOException e) {
        LOGGER.error("Error in receiving data", e);
    }       
}

【问题讨论】:

    标签: java post curl restlet


    【解决方案1】:

    如果证书是自签名证书(似乎是这样),您应该查看 Restlet 文档中的此页面:http://restlet.com/technical-resources/restlet-framework/guide/2.3/core/security/https

    希望对你有帮助 蒂埃里

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-11
      • 1970-01-01
      • 2016-03-08
      • 2017-03-19
      • 2017-05-25
      • 1970-01-01
      • 2023-03-19
      相关资源
      最近更新 更多