首页 文章

谷歌 Cloud 平台:无法启动java https服务器

提问于
浏览
0

以下简单程序:

import com.sun.net.httpserver.HttpsServer;
import java.net.InetSocketAddress;

class SimpleServer {
  public static void main(String[] pArgs) {
    try {
      HttpsServer s = HttpsServer.create(new InetSocketAddress(443), 0);
      System.out.println(" " + s);
    } catch (Exception pE) {
      throw new RuntimeException("Could not create HTTPS server", pE);  
    }
  }
}

将无法在谷歌 Cloud 平台(Google Compute Engine - IaaS)托管的Debian VM中运行:

Exception in thread "main" java.lang.RuntimeException: Could not create HTTPS server
    at SimpleServer.main(SimpleServer.java:10)
Caused by: java.net.SocketException: Permission denied
    at sun.nio.ch.Net.bind0(Native Method)
    at sun.nio.ch.Net.bind(Net.java:433)
    at sun.nio.ch.Net.bind(Net.java:425)
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
    at sun.net.httpserver.ServerImpl.<init>(ServerImpl.java:100)
    at sun.net.httpserver.HttpsServerImpl.<init>(HttpsServerImpl.java:50)
    at sun.net.httpserver.DefaultHttpServerProvider.createHttpsServer(DefaultHttpServerProvider.java:39)
    at com.sun.net.httpserver.HttpsServer.create(HttpsServer.java:90)
    at SimpleServer.main(SimpleServer.java:7)

此示例在Windows桌面上运行时也有效,如果我们将端口从443更改为其他内容 . 那么我们如何强制谷歌 Cloud 允许443上的服务器?我试过打开防火墙,但这不起作用(不是它真的应该:-(因为它是一个单独的问题) .

java版本是(虽然我怀疑这很重要):

openjdk version "1.8.0_141"
OpenJDK Runtime Environment (build 1.8.0_141-8u141-b15-1~deb9u1-b15)
OpenJDK 64-Bit Server VM (build 25.141-b15, mixed mode)

1 回答

  • 1

    这不是GCP问题,而是Linux / Unix安全功能 .

    linux / Unix上1024以下的端口是“特权端口”,需要提升权限才能创建 .

    当您在GCP中时,您有几种选择 .

    使用1024以上的高端口作为非特权用户,并且:

    • 连接到网址https:// foo:8443中的高端口

    • 使用GCP网络或HTTP / HTTPS负载均衡器将端口443转发到高端口

    • 利用IP表将数据包从443转发到VM实例内的高端口

    • 使用suid,sudo或其他方法运行服务

    • 将_CAP_NET_BIND_SERVICE_功能(7)授予进程 .

    最后两个选项具有复杂的安全隐患,如果可能的话应该避免 .

    以下链接将涵盖上述一些选项,但第一个选项将是最简单的,同时比最后两个选项更安全 .

    https://debian-administration.org/article/386/Running_network_services_as_a_non-root_user .

相关问题