为什么在 undertow 服务器中将端口设置为 80 失败? (java)

why set port to 80 failed in undertow server? (java)

我"mvn exec:java"程序java的时候,设置端口80有问题,但是8080可以成功。

 Undertow server = Undertow.builder()
                    .addHttpListener(80, "localhost")
                    .setHandler(path)
                    .build();
            server.start();

当我将其设置为 80 时,我遇到了异常

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.mojo.exec.ExecJavaMojo.run(ExecJavaMojo.java:297)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: java.net.SocketException: Permission denied
        at io.undertow.Undertow.start(Undertow.java:141)
        at cc.cmu.edu.Q1.Q1Controller.main(Q1Controller.java:46)
        ... 6 more
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.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:67)
        at org.xnio.nio.NioXnioWorker.createTcpConnectionServer(NioXnioWorker.java:175)
        at org.xnio.XnioWorker.createStreamConnectionServer(XnioWorker.java:242)
        at io.undertow.Undertow.start(Undertow.java:120)
        ... 7 more

您需要 root 访问权限才能侦听 1024 以下的所有端口。 以 root 身份或使用 sudo 命令启动您的应用程序。