小编典典

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

spring-boot

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

http://localhost:8081

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

http://192.168.0.93:8081

当我登录到本地计算机并尝试http://192.168.0.93:8081时,它不起作用。

我想从另一台计算机通过其IP地址访问UI,但它不起作用。

http://192.168.0.93:8081

当我找到springs文档时,我发现您可以添加要让Tomcat设置使用server.address的Web服务器的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

阅读 668

收藏
2020-05-30

共1个答案

小编典典

既然有这么多人看过这个问题。解决方案是确保在托管的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
2020-05-30