简体   繁体   中英

Unable to start embedded Jetty server

I have made a spring boot project.

When I make a jar using windows 10 and run on my windows machine it works without a problem.

When I try running it on either Debian 10 or CentOS 8 with Java 8 installed I get

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-02-02 20:22:13.050 ERROR 14537 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.boot.web.server.WebServerException: Unable to start embedded Jetty server
        at org.springframework.boot.web.embedded.jetty.JettyWebServer.start(JettyWebServer.java:165) ~[spring-boot-2.1.9.RELEASE.jar!/:2.1.9.RELEASE]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.startWebServer(ServletWebServerApplicationContext.java:297) ~[spring-boot-2.1.9.RELEASE.jar!/:2.1.9.RELEASE]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:163) ~[spring-boot-2.1.9.RELEASE.jar!/:2.1.9.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:552) ~[spring-context-5.1.10.RELEASE.jar!/:5.1.10.RELEASE]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.1.9.RELEASE.jar!/:2.1.9.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:744) [spring-boot-2.1.9.RELEASE.jar!/:2.1.9.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:391) [spring-boot-2.1.9.RELEASE.jar!/:2.1.9.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) [spring-boot-2.1.9.RELEASE.jar!/:2.1.9.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.1.9.RELEASE.jar!/:2.1.9.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204) [spring-boot-2.1.9.RELEASE.jar!/:2.1.9.RELEASE]
        at ptoject.web.Application.main(Application.java:14) [classes!/:na]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_232]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_232]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_232]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_232]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [web-4.jar:na]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [web-4.jar:na]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:51) [web-4.jar:na]
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:52) [web-4.jar:na]
Caused by: java.net.SocketException: Permission denied
        at sun.nio.ch.Net.bind0(Native Method) ~[na:1.8.0_232]
        at sun.nio.ch.Net.bind(Net.java:433) ~[na:1.8.0_232]
        at sun.nio.ch.Net.bind(Net.java:425) ~[na:1.8.0_232]
        at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) ~[na:1.8.0_232]
        at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74) ~[na:1.8.0_232]
        at org.eclipse.jetty.server.ServerConnector.openAcceptChannel(ServerConnector.java:342) ~[jetty-server-9.4.19.v20190610.jar!/:9.4.19.v20190610]
        at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:308) ~[jetty-server-9.4.19.v20190610.jar!/:9.4.19.v20190610]
        at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80) ~[jetty-server-9.4.19.v20190610.jar!/:9.4.19.v20190610]
        at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:236) ~[jetty-server-9.4.19.v20190610.jar!/:9.4.19.v20190610]
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) ~[jetty-util-9.4.19.v20190610.jar!/:9.4.19.v20190610]
        at org.springframework.boot.web.embedded.jetty.JettyWebServer.start(JettyWebServer.java:146) ~[spring-boot-2.1.9.RELEASE.jar!/:2.1.9.RELEASE]

If it is any help this is my build.gradle file

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.1.9.RELEASE")
    }
}

plugins {
    id 'org.springframework.boot' version '2.1.9.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
    id 'application'
}

group = 'project'
version = '4'
mainClassName = "project.web.Application"

repositories {
    mavenCentral()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web") {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
    }
    compile('org.springframework.boot:spring-boot-starter-data-jpa') {
        exclude group: 'org.apache.tomcat', module: 'tomcat-jdbc'
    }
    implementation 'org.springframework.boot:spring-boot-starter-jetty'
    implementation 'org.springframework.boot:spring-boot-starter-freemarker'
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-mail';
    implementation 'com.h2database:h2'
    implementation 'org.webjars:jquery:3.1.0'
    implementation 'commons-validator:commons-validator:1.6'
}

jar {
    // include all the jars
    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
        (configurations.runtime).collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
    manifest {
        attributes 'Main-Class': 'web.Application'
    }
}

I am at a complete loss as to why this is happening

Debian 10 and CentOS 8 has

openjdk version "1.8.0_232"
OpenJDK Runtime Environment (build 1.8.0_232-b09)
OpenJDK 64-Bit Server VM (build 25.232-b09, mixed mode)

And windows machine has

java version "1.8.0_241"
Java(TM) SE Runtime Environment (build 1.8.0_241-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.241-b07, mixed mode)

All ports below 1024 are only allowed to be opened by root. Thats why spring boot applications usually are run at port 8080 and instead you have some sort of proxy/loadbalancer redirecting the traffic from 80 to 8080.

Why you don't want to run an application as root, is because if you have a vulnerability/bug in your application that allows for "remote code execution" (someone can execute malicious code through your application) then that piece of code will be run as root, which is the most powerful user on the linux server.

Then the attacker can basically do whatever they want on the server/machine.

There are several recommendations in this thread how to solve your problem https://serverfault.com/questions/112795/how-to-run-a-server-on-port-80-as-a-normal-user-on-linux

But i will never recommend anyone to run an application as root, it is a major security risk. I rather run it on a different port (like 8080).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM