Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Http to https redirection [closed]

Tags:

java

http

We have a Website that can be accessed with both http and https

We need all the pages to be accessed with http which is working fine but when users logged into the Site we need all the pages that had authenticated need to display with https

Please let us know what is the easiest way to achieve this

Thanks Srinivas

like image 643
Srinivas Avatar asked Aug 09 '11 05:08

Srinivas


1 Answers

You could use a filter:

public class MyFilter implements Filter {
    private FilterConfig conf;

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest)request;
        HttpServletResponse resp = (HttpServletResponse)response;
        if (req.getRemoteUser() != null && req.getScheme().equals("http")) {
            String url = "https://" + req.getServerName()
                    + req.getContextPath() + req.getServletPath();
            if (req.getPathInfo() != null) {
                url += req.getPathInfo();
            }
            resp.sendRedirect(url);
        } else {
            chain.doFilter(request, response);
        }
    }

    public FilterConfig getFilterConfig() {
        return conf;
    }

    public void setFilterConfig(FilterConfig filterConfig) {
        conf = filterConfig;
    }

    public void destroy() {        
    }

    public void init(FilterConfig filterConfig) {
        conf = filterConfig;
    }
}
like image 147
Maurice Perry Avatar answered Nov 15 '22 18:11

Maurice Perry