镜像快速入门 java - 连接被拒绝

Posted

技术标签:

【中文标题】镜像快速入门 java - 连接被拒绝【英文标题】:mirror quickstart java - Connection Refused 【发布时间】:2014-03-18 18:10:13 【问题描述】:

尝试在 apache tomcat 7 上设置 google glass quickstart 项目。当我尝试启动它时它工作正常:http://localhost:8080 当我在我的家庭网络中时。但是当我在公司网络中时它不起作用。

在尝试交换访问令牌之前,我已经在 context.xml 和代码中设置了代理配置。我在源代码中的 AuthServlet 类中收到连接被拒绝异常。我已经修改了源代码以在这里定义代理设置的系统变量。

当我在设置代理后尝试连接到网络上的任何 url 时,我能够连接到互联网,但令牌请求的交换失败,不知道为什么会发生这种情况。有人可以指出我正确的方向吗?

下面是显示代理设置的代码:

public class AuthServlet extends HttpServlet 
  private static final Logger LOG = Logger.getLogger(AuthServlet.class.getSimpleName());

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException 
      System.out.println("inside authservlet");
      System.setProperty("http.proxyHost", "proxy server");
      System.setProperty("http.proxyPort", "proxy server port");
      Authenticator.setDefault(
              new Authenticator() 
                  public PasswordAuthentication getPasswordAuthentication() 
                      return new PasswordAuthentication(
                              "username", "password".toCharArray());
                  
              
              );
      System.setProperty("http.proxyUser", "username");
      System.setProperty("http.proxyPassword", "password");
      System.out.println("finished proxy set up");
      try 
        URL myURL = new URL("http://google.com");
        URLConnection myURLConnection = myURL.openConnection();
        myURLConnection.connect();
     catch (MalformedURLException e) 
        e.printStackTrace();
     catch (IOException e) 
        e.printStackTrace();
     catch (Exception e) 
        e.printStackTrace();
    
      System.out.println("trying to connect to a URL");
    // If something went wrong, log the error message.
    if (req.getParameter("error") != null) 
      LOG.severe("Something went wrong during auth: " + req.getParameter("error"));
      res.setContentType("text/plain");
      res.getWriter().write("Something went wrong during auth. Please check your log for details");
      return;
    

    // If we have a code, finish the OAuth 2.0 dance
    if (req.getParameter("code") != null) 
      System.out.println("Got a code. Attempting to exchange for access token.");

      AuthorizationCodeFlow flow = AuthUtil.newAuthorizationCodeFlow();
      TokenResponse tokenResponse =
          flow.newTokenRequest(req.getParameter("code"))
              .setRedirectUri(WebUtil.buildUrl(req, "/oauth2callback")).execute();
      //Error is in the above line.

      // Extract the Google User ID from the ID token in the auth response
      String userId = ((GoogleTokenResponse) tokenResponse).parseIdToken().getPayload().getUserId();

以下是异常的日志

inside AuthFilter
sending redirect
inside AuthFilter
Skipping auth check during auth flow
inside authservlet
finished proxy set up
trying to connect to a URL
No auth context found. Kicking off a new auth flow.
inside AuthFilter
sending redirect
inside AuthFilter
Skipping auth check during auth flow
inside authservlet
finished proxy set up
trying to connect to a URL
No auth context found. Kicking off a new auth flow.
inside AuthFilter
Skipping auth check during auth flow
inside authservlet
finished proxy set up
trying to connect to a URL
Got a code. Attempting to exchange for access token.
Feb 18, 2014 1:04:20 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [oauth2callback] in context with path [] threw exception
java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(Unknown Source)
    at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:77)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:965)
    at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:283)
    at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute(GoogleAuthorizationCodeTokenRequest.java:158)
    at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute(GoogleAuthorizationCodeTokenRequest.java:79)
    at com.google.glassware.AuthServlet.doGet(AuthServlet.java:86)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.google.glassware.ReauthFilter.doFilter(ReauthFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.google.glassware.AuthFilter.doFilter(AuthFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

【问题讨论】:

【参考方案1】:

问题在于您正在设置 HTTP 代理连接,但 OAuth 交换使用 HTTPS 连接。

至少,您需要为 HTTPS 设置代理信息,例如

systemProperties.setProperty( "https.proxyHost", "proxy server" );
systemProperties.setProperty( "https.proxyPort", "secure proxy port" );

您应该确保通过与https://google.com/ 的连接来测试它

不过,这是您可能需要做的最少的事情。现实情况是,它可以变得复杂得多。 HTTPS 确实不是通过代理工作的,代理从根本上破坏了它背后的整个安全性。您可能需要设置许多其他东西,以便故意破坏安全控制,您真的不应该这样做。 (但如果你必须这样做,看起来How do a send an HTTPS request through a proxy in Java? 有几个指示你需要做的事情。)

但老实说,这只是你的问题的开始。尽管这意味着您将能够通过代理发送内容,但 Mirror API 使用 web-hook 作为其几个函数的回调,并且这些回调也将通过 HTTPS。因此,您的代理还需要能够处理传入 HTTPS 连接并将它们路由到您的服务器。

您可能需要考虑在 App Engine 上进行测试,因为这些问题会为您处理。

【讨论】:

感谢您的快速响应。将代理设置更改为 https 后,它起作用了,我能够过去并从公司网络内部查看快速入门项目的索引页面。

以上是关于镜像快速入门 java - 连接被拒绝的主要内容,如果未能解决你的问题,请参考以下文章

Vue的快速入门

java学习笔记之java如何快速入门

Spring IOC核心功能快速入门

快速入门:Java 连接使用 时序数据库 TimescaleDB

快速入门:Java 连接使用 时序数据库 TimescaleDB

Docker快速入门——Docker镜像制作