Java 如何设置 spring-boot 以允许从外部 IP 地址访问网络服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37445626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to setup spring-boot to allow access to the webserver from outside IP addresses
提问by ALM
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.
我一直在研究如何在tomcat
内部spring-boot
进行设置以允许来自外部 IP 地址的访问。目前我可以从 locahost:port 查看 UI,但我无法从其他系统访问它。
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:8081it does not work.
当我登录到本地计算机并尝试http://192.168.0.93:8081 时,它不起作用。
I want to access the UI from another computer via its IP address it does not work.
我想通过其 IP 地址从另一台计算机访问 UI,但它不起作用。
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.
当我找到springs
文档时,我发现您可以添加您希望 tomcat 在其上设置网络服务器以使用 server.address 的 IP 地址。这应该允许外部系统通过这个 IP 地址访问服务器。
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.
如果我不提供地址,端口工作正常,但是当我提供地址并尝试运行它时,我遇到了绑定到该地址的问题。看起来spring-boot
已经分配了 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?
如何正确设置spring-boot
以允许外部地址访问 tomact 并在本地访问时识别其自己的 IP?
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
采纳答案by ALM
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.
既然这么多人看过这个问题。解决方法是确保在托管 CentOS 机器上正确配置防火墙,而不是明确设置服务器地址。
INCORRECT SETUP
不正确的设置
This failedpreviously 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.
正确设置防火墙后,您无需指定 server.address 仅指定端口。
CORRECT SETUP
正确设置
server.port=8081
This allowed me to access the application correctly from other systems using its ip.
这使我可以使用其 ip 从其他系统正确访问该应用程序。
http://<someip>:<server.port>
http://192.168.0.93:8081
回答by Minjeong Choi
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)
我使用firewalld在我的vm(centos7)中添加我的springboot webserver端口
My webserver was using 8080, so I did:
我的网络服务器使用的是 8080,所以我做了:
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,
您需要重新启动/重新加载 firewalld 以应用更改。如果你,
sudo firewall-cmd --list-all*
you can find 8080/tcp
has been added to ports list
您可以找到8080/tcp
已添加到端口列表