修改为https连接 #163

Merged
p95fco63j merged 6 commits from junmao_branch into develop 1 week ago

@ -0,0 +1,46 @@
package com.campus.water.config;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import java.io.IOException;
@Configuration
public class HttpsConfig {
// 优先级最高的过滤器实现HTTP→HTTPS跳转
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public Filter httpToHttpsRedirectFilter() {
return new Filter() {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
// 仅处理HTTP请求跳转到HTTPS
if ("http".equals(req.getScheme())) {
String httpsUrl = "https://" + req.getServerName() + ":" + 8081 + req.getRequestURI();
if (req.getQueryString() != null) {
httpsUrl += "?" + req.getQueryString();
}
res.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
res.setHeader("Location", httpsUrl);
return;
}
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig filterConfig) {}
@Override
public void destroy() {}
};
}
}

@ -35,4 +35,12 @@ server:
charset: UTF-8
enabled: true
force: true
port: 8080
port: 8081
ssl:
key-store: classpath:keystore.p12
key-store-password: 123456
key-store-type: PKCS12
key-alias: myhttps
additional-ports:
- port: 8080
protocol: HTTP
Loading…
Cancel
Save