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

发布于 2025-01-07 21:08:03 字数 1326 浏览 1 评论 0原文

我的应用程序中有几个页面在 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。

I have few pages in my application which run on https://localhost:8181 and few pages which run on http://localhost:8080.I want to downshift from https to http but not sure how to go about it.I know I have to use filter which manages the redirection from https to http and http to https.

I looked into this link [How to downshift from HTTPS to HTTP in your web application][1]

[1]: https://blogs.oracle.com/jluehe/entry/how_to_downshift_from_https but the link where the filters s used are broken..I do not have a clear idea as to what is supposed to be done inside the filters...

In my 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>`

CAn some one guide me what needs to be done inside the filter so that the shifting between https to http is smooth.

I am using Glassfish 3.1.1 and JSF.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

第几種人 2025-01-14 21:08:03
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);  
        }  
    }  
}
}
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);  
        }  
    }  
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文