java - 使用 google map api 时获取 "peer not authenticated"

标签 java google-maps-api-3 httpclient

我在创建 javafx 包后使用 google map api 得到“peer not authenticated”。 直接从代码或可执行 jar 文件运行时它工作正常,以下是我得到的异常,

javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
   at sun.security.ssl.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:397)
   at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:128)
   at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:390)
   at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:148)
   at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:149)
   at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:121)
   at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:561)
   at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:415)
   at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
   at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
   at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:732)
   at com.precisionhawk.flightplanner.utils.LocationUtils.performAPICall(LocationUtils.java:109)
   at com.precisionhawk.flightplanner.utils.LocationUtils.getAutocompleteResults(LocationUtils.java:74)
   at com.precisionhawk.flightplanner.controls.AddressTextField$1$1.run(AddressTextField.java:103)
   at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
   at java.util.concurrent.FutureTask.run(FutureTask.java:166)
   at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:178)
   at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:292)

我正在使用 http 客户端调用 api。我试过绕过 X509 证书,但还是不行

下面是我的代码

SSLContext ctx = null;
 try {

            SSLUtilities.trustAllHostnames();
            SSLUtilities.trustAllHttpsCertificates();

            HttpClient httpClient = new DefaultHttpClient();

            HttpGet getRequest = new HttpGet(url.toString());
            getRequest.addHeader("accept", "application/json");

            HttpResponse response = httpClient.execute(getRequest);

            if (response.getStatusLine().getStatusCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + response.getStatusLine().getStatusCode());
            }
            BufferedReader br = new BufferedReader(
                    new InputStreamReader((response.getEntity().getContent())));
            String inputLine;

            while ((inputLine = br.readLine()) != null) {
                result = result + inputLine;
            }
            br.close();
            LOG.info("Autocomplete API call Response: " + result);
            }
            catch (Exception e) {
                LOG.error("Autocomplete  Exception: "+e.getMessage());
            }

最佳答案

如果“创建 javafx 包”是指创建 Self-Contained Application , 那么这样做的一个原因是 sun 加密扩展没有包含在默认的独立应用程序包中,如 JavaFX deployment blog 中详述的那样。 .

从部署博客中引用,有关于将 sun jce 提供程序包含在 JavaFX 2.2 的自包含应用程序中的说明。对于 JavaFX 部署工具的 future 版本,可能会提供更方便的方法来执行此操作。

Fine tuning application bundle

If you are using packaging tools to produce an installable package there could be a need to tweak the application image before it is wrapped into the installer. Why? For example you may want to sign the application, so it does not appear to be untrusted to the OS (for example to please Mac OS X Gatekeeper).

Also by default a self-contained application does not contain full copy of Java Runtime. We only include set of mandatory components. Part of the reason why this approach was taken is that we want to reduce the package size. However, there are situations where your application may depend on these optional components and in that case you will need a way to add them to the private runtime. For example https connections will not work if jre/lib/ext/sunjce_provider.jar is missing.

Currently this can be achieved by providing a custom config script that is executed after application image is populated. Like in the example above with the icon, you need to enable verbose output to find the name of the script file and then drop it to the location where packaging tools will find it. Note that scripting language is platform specific too. Currently we only support shell for Mac/Linux and Windows Script on windows.

How do you find out where the application image is located? Currently custom scripts are run in the directory where config files are stored but application image can be accessed using relative platform specific path. You can derive this path from verbose output or by setting environment variable JAVAFX_ANT_DEBUG to true to keep intermediate build artifacts.

Here is sample script (contributed by John Petersen) you can use to add jre/lib/ext/sunjce_provider.jar to the application package of MyApp on the Windows platform. Script using Javascript but you could also use VBScript for Windows scripting.

<?xml version="1.0" ?>  
<package>  
   <job id="postImage">  
    <script language="JScript">  
     <![CDATA[  
        var oFSO = new ActiveXObject("Scripting.FileSystemObject");  
        var oFolder = oFSO.getFolder(".");  
        var from = oFolder.path + "\\MyApp\\app\\sunjce_provider.jar";  
        var to = oFolder.path + "\\MyApp\\runtime\\jre\\lib\\ext";  
        if (!oFSO.FolderExists(to)) {  
          oFSO.CreateFolder(to);  
        }  
        to += "\\";  
        oFSO.CopyFile(from, to);  
     ]]>  
    </script>  
   </job>  
</package>

I am using maven. so how should i call this script from pom.xml that will create the jar within jre/lib/ext folder?

我不是专家。上面的解决方案是针对 apache ant 的。如果您希望继续使用 Maven,则需要针对此问题制定自己的解决方案。

关于java - 使用 google map api 时获取 "peer not authenticated",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16299126/

相关文章:

java - 在 Java 中滑动 DFT?

Java 使用字符串作为对象调用

java - 如何使 jquery 行不可编辑并使用 ajax 调用将同一行保存在数据库中?

java - 如何在 Java 中从字符串动态创建 Groovy 闭包

jquery - Google map 标记不在中心

c# - HttpClient HttpRequestException

javascript - 通过Javascriptalert()和console.log()显示对象时显示不同的输出

javascript - 谷歌地图绘制多边形并缩放到其边界

java - 如何在httpget中设置用户名/密码

java - 异常已被其他异常错误捕获