Spring Boot Jetty配置HTTP(80端口)自动重定向至HTTPS(8443端口)
搞定Jetty 80端口无法访问 + HTTP自动重定向到HTTPS的问题
先解决80端口访问失败的核心问题
你控制台显示80端口启动了但连不上,十有八九是权限问题!因为系统里1024以下的端口(比如80)需要管理员/root权限才能监听,普通用户运行gradlew bootRun根本绑定不了这个端口,日志只是告诉你Jetty尝试启动了,但实际没成功。
验证方法:
- Windows:打开命令提示符,运行
netstat -ano | findstr :80,看看有没有Jetty进程在监听80 - Linux/macOS:终端里跑
lsof -i :80,检查80端口的占用情况
临时解决权限问题:
- Linux/macOS:直接用
sudo ./gradlew bootRun启动 - Windows:右键终端/命令提示符,选「以管理员身份运行」,再执行
gradlew bootRun
等你确认80端口能正常监听了,再搞重定向的事儿。
实现HTTP(80)自动跳转到HTTPS(8443)
这里给你两种方案,看你项目有没有用Spring Security来选:
方案1:用Spring Security一键配置(推荐,简单省心)
如果你的项目已经引入Spring Security,直接加个配置类就行:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.web.SecurityFilterChain; @Configuration public class HttpsRedirectConfig { @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { // 强制所有请求都跳转到HTTPS http.requiresChannel(channel -> channel.anyRequest().requiresSecure() ); // 如果不需要其他权限控制,直接允许所有请求访问 http.authorizeHttpRequests(auth -> auth.anyRequest().permitAll() ); return http.build(); } }
方案2:自定义Jetty Connector(适合没用到Spring Security的项目)
如果没引入Spring Security,就自己写Jetty的自定义配置来实现重定向:
import org.eclipse.jetty.server.*; import org.eclipse.jetty.server.handler.HandlerWrapper; import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @Configuration public class JettyHttpRedirectConfig { @Bean public WebServerFactoryCustomizer<JettyServletWebServerFactory> jettyHttpToHttpsCustomizer() { return factory -> { factory.addServerCustomizers(server -> { // 先找到已经配置好的HTTPS连接器(8443端口) Connector httpsConnector = null; for (Connector connector : server.getConnectors()) { if (connector instanceof ServerConnector && ((ServerConnector) connector).getPort() == 8443) { httpsConnector = connector; break; } } if (httpsConnector != null) { // 创建HTTP连接器,绑定80端口 ServerConnector httpConnector = new ServerConnector(server); httpConnector.setPort(80); // 添加重定向处理器,把所有HTTP请求转到HTTPS HandlerWrapper redirectHandler = new HandlerWrapper() { @Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String httpsUrl = "https://" + request.getServerName() + ":8443" + request.getRequestURI(); // 带上查询参数 if (request.getQueryString() != null) { httpsUrl += "?" + request.getQueryString(); } response.sendRedirect(httpsUrl); baseRequest.setHandled(true); } }; server.setHandler(redirectHandler); // 把HTTP连接器添加到Jetty服务器 server.addConnector(httpConnector); } }); }; } }
最后验证效果
- 用管理员/root权限启动应用,确认控制台输出里80和8443端口都正常启动了
- 访问
http://localhost:80,应该会自动跳转到https://localhost:8443,页面也能正常加载
要是不想折腾80端口的权限,也可以把HTTP端口改成8080(普通用户就能用),然后把配置里的80换成8080就行,效果是一样的~
内容的提问来源于stack exchange,提问作者Vinay
相关产品推荐
相关产品推荐

