Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup spring-boot to allow access to the webserver from outside IP addresses

I have been looking into how to setup tomcat within spring-boot to allow access from outside IP addresses. Currently I can view the UI from locahost:port but I can not access this from other systems.

http://localhost:8081 

When I am logged into the local computer it works.

http://192.168.0.93:8081

When I am logged into the local computer and try http://192.168.0.93:8081 it does not work.

I want to access the UI from another computer via its IP address it does not work.

http://192.168.0.93:8081

When I got to springs documentation I found you can add the IP address on which you want tomcat to set the webserver for using server.address. This should then allow the server to be accessed via this IP address by outside systems.

server.port=8082
server.address=192.168.0.93

The port works fine if I do not supply the address but when I supply the address and try to run it I run into an issue for binding to that address. It looks like spring-boot has already assigned locahost:8082.

Question:

How do I correctly setup spring-boot to allow tomact to be accessed by outside addresses and recognize its own IP when accessed locally?

Thank you

Stack Trace:

java.net.BindException: Cannot assign requested address
  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 org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:340)
  at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:765)
  at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:473)
  at org.apache.catalina.connector.Connector.startInternal(Connector.java:986)
  at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
  at org.apache.catalina.core.StandardService.addConnector(StandardService.java:239)
  at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.addPreviouslyRemovedConnectors(TomcatEmbeddedServletContainer.java:194)
  at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:151)
  at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:293)
  at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:141)
  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
  at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
  at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766)
  at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)
  at com.miw.mcb.server.ReactAndSpringDataRestApplication.main(ReactAndSpringDataRestApplication.java:18)
  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.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:478)
  at java.lang.Thread.run(Thread.java:745)
2016-05-25 11:24:30 - Failed to start connector [Connector[HTTP/1.1-8081]]
org.apache.catalina.LifecycleException: Failed to start component [Connector[HTTP/1.1-8081]]
  at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153)
  at org.apache.catalina.core.StandardService.addConnector(StandardService.java:239)
  at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.addPreviouslyRemovedConnectors(TomcatEmbeddedServletContainer.java:194)
  at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:151)
  at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:293)
  at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:141)
  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
  at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
  at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766)
  at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)
  at com.miw.mcb.server.ReactAndSpringDataRestApplication.main(ReactAndSpringDataRestApplication.java:18)
  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.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:478)
  at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.catalina.LifecycleException: service.getName(): "Tomcat";  Protocol handler start failed
  at org.apache.catalina.connector.Connector.startInternal(Connector.java:993)
  at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
  ... 19 common frames omitted
Caused by: java.net.BindException: Cannot assign requested address
  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 org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:340)
  at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:765)
  at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:473)
  at org.apache.catalina.connector.Connector.startInternal(Connector.java:986)
  ... 20 common frames omitted
2016-05-25 11:24:30 - Pausing ProtocolHandler ["http-nio-192.168.0.93-8081"]
2016-05-25 11:24:30 - Stopping service Tomcat
2016-05-25 11:24:30 - The stop() method was called on component [StandardServer[-1]] after stop() had already been called. The second call will be ignored.
2016-05-25 11:24:30 - Stopping ProtocolHandler ["http-nio-192.168.0.93-8081"]
2016-05-25 11:24:30 - Destroying ProtocolHandler ["http-nio-192.168.0.93-8081"]
2016-05-25 11:24:30 - Application startup failed
org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat servlet container
  at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:165)
  at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:293)
  at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:141)
  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
  at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
  at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766)
  at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)
  at com.miw.mcb.server.ReactAndSpringDataRestApplication.main(ReactAndSpringDataRestApplication.java:18)
  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.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:478)
  at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: Tomcat connector in failed state
  at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:159)
  ... 16 common frames omitted
like image 373
ALM Avatar asked May 25 '16 19:05

ALM


2 Answers

Since so many people have viewed this question. The resolution was to make sure the firewall was configured correctly on the hosting CentOS machine and not set the server address explicitly.

INCORRECT SETUP

This failed previously with the firewall incorrectly setup

server.port=8081
server.address=192.168.0.93

Once the firewall is correctly setup you do not need to specify the server.address just the port.

CORRECT SETUP

server.port=8081

This allowed me to access the application correctly from other systems using its ip.

http://<someip>:<server.port>
http://192.168.0.93:8081
like image 192
ALM Avatar answered Oct 07 '22 22:10

ALM


Thank you, you saved me tons! I wanted to post this in comments, but I don't have enough reputation to reply.

Those of you who want the information regarding firewall changes,

I used firewalld to add my springboot webserver ports in my vm(centos7)

My webserver was using 8080, so I did:

firewall-cmd --permanent --reload --zone=public --add-port=8080/tcp*

sudo systemctl restart firewalld*

you need to restart/reload the firewalld to apply the changes. if you,

sudo firewall-cmd --list-all*

you can find 8080/tcp has been added to ports list

like image 36
Minjeong Choi Avatar answered Oct 07 '22 22:10

Minjeong Choi