上面的答案对我不起作用,因为我没有使用这样的文件。我最终做了一个这样的后端过滤器:
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class HttpToHttpsFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
// Nothing to destroy
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
String protocol = httpRequest.getHeader("X-Forwarded-Proto");
if (protocol != null) {
if(protocol.toLowerCase().equals("https")) {
httpResponse.setHeader("Strict-Transport-Security", "max-age=60");
}
else if(protocol.toLowerCase().equals("http")) {
String host = httpRequest.getServerName();
String requestURI = httpRequest.getRequestURI();
String redirectUrl = "https://" + host + requestURI;
httpResponse.sendRedirect(redirectUrl);
return;
}
}
chain.doFilter(request, response);
}
}