如何检查Spring Boot应用是否启用HTTPS并在启动时打印指定格式的应用详情
解决Spring Boot启动时打印应用详情URL的问题
我来帮你搞定这个需求!在Spring Boot里,我们可以通过两种可靠的方式来判断应用是否启用HTTPS,同时获取端口和上下文路径,最终拼接出你想要的- [http|https]://localhost:port/contextPath格式。
方法一:利用ServerProperties直接读取配置
Spring Boot的ServerProperties已经封装了所有服务器相关的配置,我们可以直接注入这个Bean来判断SSL状态,再获取端口和上下文路径。你可以创建一个配置类,添加ApplicationRunner来在启动时执行打印逻辑:
import org.springframework.boot.ApplicationRunner; import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @EnableConfigurationProperties(ServerProperties.class) public class StartupUrlPrinter { @Bean public ApplicationRunner printAppUrl(ServerProperties serverProperties) { return args -> { // 判断HTTPS是否启用 boolean isHttpsEnabled = serverProperties.getSsl() != null && serverProperties.getSsl().isEnabled(); String protocol = isHttpsEnabled ? "https" : "http"; // 获取端口:HTTPS启用时优先用SSL端口,否则用默认HTTP端口 int port = isHttpsEnabled ? serverProperties.getSsl().getPort() != null ? serverProperties.getSsl().getPort() : 443 : serverProperties.getPort() != null ? serverProperties.getPort() : 8080; // 处理上下文路径,确保格式正确 String contextPath = serverProperties.getServlet().getContextPath(); contextPath = contextPath == null || contextPath.isEmpty() ? "" : contextPath.startsWith("/") ? contextPath : "/" + contextPath; // 拼接并打印URL String appUrl = String.format("%s://localhost:%d%s", protocol, port, contextPath); System.out.println("- " + appUrl); }; } }
这种方法的优势是直接读取Spring Boot自动配置的属性,不需要自己解析配置文件,能准确反映你在application.properties/application.yml或者外部配置(命令行、环境变量)里设置的值。
方法二:通过监听启动事件获取实际运行的WebServer信息
如果你想确保获取的是实际生效的服务器配置(比如有些场景下配置可能被动态修改),可以监听ApplicationStartedEvent事件,从事件中获取WebServer实例:
import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; import org.springframework.web.context.WebApplicationContext; @Component public class AppUrlStartupListener implements ApplicationListener<ApplicationStartedEvent> { @Override public void onApplicationEvent(ApplicationStartedEvent event) { WebApplicationContext webContext = event.getApplicationContext(); // 获取当前运行的WebServer org.springframework.boot.web.server.WebServer webServer = webContext.getBean(org.springframework.boot.web.server.WebServer.class); // 判断SSL是否启用 boolean isHttpsEnabled = webServer.getSsl() != null && webServer.getSsl().isEnabled(); String protocol = isHttpsEnabled ? "https" : "http"; // 获取实际运行的端口和上下文路径 int port = webServer.getPort(); String contextPath = webContext.getServletContext().getContextPath(); // 拼接并打印 String appUrl = String.format("%s://localhost:%d%s", protocol, port, contextPath); System.out.println("- " + appUrl); } }
这种方法会在应用完全启动后触发,能拿到实际运行的服务器参数,适合需要确认最终运行状态的场景。
注意事项
- 如果你的应用同时配置了HTTP和HTTPS(比如多个连接器),上面的方法只会打印一个URL。这种情况下,你需要遍历所有连接器来获取每个协议的端口,打印多个URL。
- 默认情况下,Spring Boot只会启用一个协议(HTTP或HTTPS),除非你手动配置了多个连接器。
内容的提问来源于stack exchange,提问作者user8884781
相关产品推荐
相关产品推荐

