gpt4 book ai didi

tomcat - ELB后面的tomcat如何重定向到https

转载 作者:行者123 更新时间:2023-11-28 21:55:29 25 4
gpt4 key购买 nike

我在 AWS 上有以下设置

ELB(终止 SSL)-> nginx 在 80 上接收 http 并转发到 -> 在 8080 上的 tomcat

但是,当我在我的 servlet 中执行 response.sendRedirect("/somepath") 时,浏览器将其接收为 302 http://example.com/somepath

但是我想让浏览器得到https://example.com/somepath ,如何在不设置 SSL 的情况下在 tomcat 或 nginx 中实现此目的。

最佳答案

这是因为response.sendRedirect();

SSL Offloading servers actually cause this problem of https URLs getting converted into http URLs in case of sendRedirect. Let's try to understand how it all happens. When an https URL reaches an intermediate SSL Offloading server, the request is decoded (to offload the decoding task which would have been done by the actual target server otherwise) into an http request and the SSL Offloader now sends that decoded http request to the actual target server (it may go through few other intermediary servers for other purposes as well). Now, the target server has no clue whether it was originally an https request or not and it treats it as an http request only. This is the reason why a servlet calling sendRedirect on the target server will result into an http URL for the redirected resource (having a relative URL)and the same http URL is sent back to the client browser. Remember, no SSL Offloading server will come into picture for this URL as it's an http request and not an https request. If the redirected resource is an absolute https URL then also an intermediate SSL Offloader may convert that https request into an http request before it reaches to the client browser.

问题的解决方案是在 HttpServletResponseWrapper 中实现一个过滤器并覆盖 sendRedirect。

过滤器。

public class AbsoluteSendRedirectFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException { }
public void destroy() { }
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain) throws IOException, ServletException {
//continue the request
chain.doFilter(request, new SendRedirectOverloadedResponse(request, response));
}
}

HttpServletResponseWrapper

public class SendRedirectOverloadedResponse extends HttpServletResponseWrapper {

private HttpServletRequest m_request;
private String prefix = null;

public SendRedirectOverloadedResponse(HttpServletRequest inRequest,
HttpServletResponse response) {
super(response);
m_request = inRequest;
prefix = getPrefix(inRequest);
}

public void sendRedirect(String location) throws IOException {
String finalurl = null;
if (isUrlAbsolute(location)) {
finalurl = location;
} else {
finalurl = fixForScheme(prefix + location);
}
super.sendRedirect(finalurl);
}

public boolean isUrlAbsolute(String url) {
return url.toLowerCase().startsWith("http");
}

public String fixForScheme(String url) {
//alter the url here if you were to change the scheme return url;
}

public String getPrefix(HttpServletRequest request) {
StringBuffer str = request.getRequestURL();
String url = str.toString();
String uri = request.getRequestURI();
int offset = url.indexOf(uri);
String prefix = url.substring(0,offset);
}
}

资源来自这里:
http://www.hoitikwong.com/2013/03/the-mystery-case-of-https-becoming-http.html
http://geekexplains.blogspot.in/2008/06/https-becoming-http-in-case-of.html

关于tomcat - ELB后面的tomcat如何重定向到https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27542488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com