尝试在apache tomcat 7上设置Google Glass快速入门项目。当我尝试启动时,它正常工作:http://localhost:8080
当我在家乡网络时。但是当我在公司网络中时,它不起作用。
我在context.xml和代码中设置了代理配置,然后才尝试交换访问令牌。我在源代码中的AuthServlet类中获得连接拒绝异常。我也修改了源代码,以便为代理设置定义系统变量。
当我在设置代理后尝试连接到网络上的任何网址时,我能够连接到互联网,但令牌请求的交换失败,不知道为什么会发生这种情况。有人可以指点我正确的方向吗?
以下是显示代理设置的代码:
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)
答案 0 :(得分: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挂钩作为其若干功能的回调,并且这些回调也将通过HTTPS进行。因此,您的代理还需要能够处理传入 HTTPS连接并将它们路由到您的服务器。
您可能希望在App Engine上进行测试,因为这些问题将由您处理。