http rest JAVA_在Java中将HTTPS与REST一起使用

我有一台使用HTTPS且使用Firefox的Grizzly制造的REST服务器。这是代码:

//Build a new Servlet Adapter.

ServletAdapter adapter=new ServletAdapter();

adapter.addInitParameter("com.sun.jersey.config.property.packages", "My.services");

adapter.addInitParameter(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS, SecurityFilter.class.getName());

adapter.setContextPath("/");

adapter.setServletInstance(new ServletContainer());

//Configure SSL (See instructions at the top of this file on how these files are generated.)

SSLConfig ssl=new SSLConfig();

String keystoreFile=Main.class.getResource("resources/keystore_server.jks").toURI().getPath();

System.out.printf("Using keystore at: %s.",keystoreFile);

ssl.setKeyStoreFile(keystoreFile);

ssl.setKeyStorePass("asdfgh");

//Build the web server.

GrizzlyWebServer webServer=new GrizzlyWebServer(getPort(9999),".",true);

//Add the servlet.

webServer.addGrizzlyAdapter(adapter, new String[]{"/"});

//Set SSL

webServer.setSSLConfig(ssl);

//Start it up.

System.out.println(String.format("Jersey app started with WADL available at "

+ "%sapplication.wadl\n",

"https://localhost:9999/"));

webServer.start();

现在,我尝试用Java达到它:

SSLContext ctx=null;

try {

ctx = SSLContext.getInstance("SSL");

} catch (NoSuchAlgorithmException e1) {

e1.printStackTrace();

}

ClientConfig config=new DefaultClientConfig();

config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(null,ctx));

WebResource service=Client.create(new DefaultClientConfig()).resource("https://localhost:9999/");

//Attempt to view the user's page.

try{

service

.path("user/"+username)

.get(String.class);

}

得到:

com.sun.jersey.api.client.ClientHandlerException: 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 com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:128)

at com.sun.jersey.api.client.Client.handle(Client.java:453)

at com.sun.jersey.api.client.WebResource.handle(WebResource.java:557)

at com.sun.jersey.api.client.WebResource.get(WebResource.java:179)

从我在网上找到的示例来看,似乎需要设置一个Truststore,然后再设置某种TrustManager。对于我这个简单的小项目,这似乎需要大量代码和设置工作。有没有更简单的方法可以说..我信任此证书并指向.cert文件?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值