如何使用curl POST正常工作的restlet进行POST调用以保护站点安全

时间:2015-04-21 07:27:59

标签: java post curl restlet

我的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

以下是提到远程网址:

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);
    }       
}

1 个答案:

答案 0 :(得分:0)

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

希望它能帮到你, 亨利