Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using filters to redirect from HTTPS to HTTP

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.

like image 813
enthusiastic Avatar asked Feb 22 '12 04:02

enthusiastic


People also ask

Does HTTPS automatically redirect to HTTP?

No. You have to explicitly redirect the HTTP traffic to HTTPS which involves configuring your web server with a rule which returns HTTP 301 status code and a location header beginning with https:// .

Is it good to redirect http to HTTPS?

Without SSL, your website will show insecure to the visitors. Therefore, using an SSL-encrypted connection for safety, accessibility or PCI compliance reasons is necessary. It becomes very important to redirect from HTTP to HTTPS.

Can we redirect HTTPS?

If you want to redirect any page at https://www.example.com to anything, then you must have a valid SSL certificate installed on the server that covers www.example.com .


1 Answers

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);  
        }  
    }  
}
}
like image 129
yogesh pathak Avatar answered Dec 25 '22 12:12

yogesh pathak