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
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With