如何设置spring-boot以允许从外部IP地址访问Web服务器

时间:2016-05-25 19:05:57

标签: java spring tomcat spring-boot webserver

我一直在研究如何在tomcat内设置spring-boot以允许从外部IP地址进行访问。目前我可以从locahost:port查看UI,但我无法从其他系统访问它。

http://localhost:8081 

当我登录到本地计算机时,它可以工作。

http://192.168.0.93:8081

当我登录本地计算机并尝试http://192.168.0.93:8081时,它无效。

我想通过其无效的IP地址从另一台计算机访问用户界面。

http://192.168.0.93:8081

当我到达springs文档时,我发现您可以添加您希望tomcat设置网络服务器以使用server.address的IP地址。然后,这应允许外部系统通过此IP地址访问服务器。

server.port=8082
server.address=192.168.0.93

如果我不提供地址,但是当我提供地址并尝试运行它时,端口工作正常我遇到绑定到该地址的问题。看起来spring-boot已经分配了locahost:8082。

问题:

如何正确设置spring-boot以允许外部地址访问tomact并在本地访问时识别自己的IP?

谢谢

堆栈跟踪:

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

解决方案

见下面的答案

4 个答案:

答案 0 :(得分:3)

因为很多人都看过这个问题。解决方案是确保在托管CentOS计算机上正确配置防火墙,而不是明确设置服务器地址。

不正确的设置

失败之前防火墙设置错误

server.port=8081
server.address=192.168.0.93

正确设置防火墙后,您无需指定server.address只是端口

正确设置

server.port=8081

这允许我使用其ip从其他系统正确访问应用程序。

http://<someip>:<server.port>
http://192.168.0.93:8081

答案 1 :(得分:2)

使用ufw

可以轻松完成此操作

要查看当前打开的端口,请输入:sudo ufw status numbered(为安全起见,请截屏)

要打开端口,请输入:sudo ufw allow 8080(8080或您想要的任何端口)

要删除端口,请输入:sudo ufw delete {number}

在删除之前,请记住使用sudo ufw status numbered仔细检查该数字。

:-)

答案 2 :(得分:0)

谢谢,您救了我很多吨! 我想在评论中发布此内容,但是我没有足够的声誉进行回复。

您想要有关防火墙更改的信息的人

我使用firewalld在我的vm(centos7)中添加了springboot Web服务器端口

我的网络服务器使用的是8080,所以我这样做了:

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

sudo systemctl restart firewalld*

您需要重新启动/重新加载Firewalld才能应用更改。

sudo firewall-cmd --list-all*

您会发现8080/tcp已添加到端口列表中

答案 3 :(得分:0)

这个问题是你需要打开端口的防火墙引起的。我正在使用 ubuntu。这个命令为我打开了端口。我的应用程序使用端口 8000 所以我使用下面的命令打开端口

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

然后使用命令重启防火墙服务

sudo systemctl restart firewalld

还要检查你是否在springboot应用程序中设置了server.servlet.context-path以知道正确的端点。如果没有直接去端口