gpt4 book ai didi

ssl - 使用过滤器从 HTTPS 重定向到 HTTP

转载 作者:太空宇宙 更新时间:2023-11-03 12:45:46 25 4
gpt4 key购买 nike

我的应用程序中有几个页面在 https://localhost:8181 上运行和在 http://localhost:8080.I 上运行的几页想从 https 降档到 http 但不确定如何去做。我知道我必须使用过滤器来管理从 https 到 http 和 http 到 https 的重定向。

我查看了这个链接 [How to downshift from HTTPS to HTTP in your web application][1]

[1]: https://blogs.oracle.com/jluehe/entry/how_to_downshift_from_https但是使用过滤器的链接已损坏..我不清楚过滤器内部应该做什么...

在我的 web.xml 中`

  <filter>
<filter-name>Non SSL port</filter-name>
<filter-class>SSLFilter</filter-class>
<init-param>
<param-name>httpPort</param-name>
<param-value>8080</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>NON SSL pages</filter-name>
<url-pattern>/pages/success.xhtml</url-pattern>
<url-pattern>/pages/failure.xhtml</url-pattern>
<url-pattern>/pages/about.xhtml</url-pattern>
</filter-mapping>`

有人可以指导我在过滤器内部需要做什么,以便在 https 到 http 之间顺利切换。

我正在使用 Glassfish 3.1.1 和 JSF。

最佳答案

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HttpsRedirectFilter implements Filter {

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {

if(request instanceof HttpServletRequest
&& response instanceof HttpServletResponse) {
HttpServletRequest httpReq = (HttpServletRequest) request;
String redirectTarget = httpReq.getRequestURL().toString();
redirectTarget = redirectTarget.replaceFirst("https", "http")
redirectTarget = redirectTarget.replaceFirst(":8443", ":8080");
redirectTarget = redirectTarget.replaceFirst("home", "home.do");
if(request.isSecure()) {
((HttpServletResponse)response).sendRedirect(redirectTarget);
} else {
chain.doFilter(request, response);
}
}
}
}

关于ssl - 使用过滤器从 HTTPS 重定向到 HTTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9389211/

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