0

I made a listener, which sets secure flag on cookie, according set secure flag on cookie programatically And I want to enable or disable this flag automatically as in secure flag on session cookies in a java How to get request from ServletContextEvent? Is it other way to do it?

public class SecureCookieServletContextListener implements ServletContextListener {
    private static final String LB_HTTPS_HEADER = "Front-End-Https";

    @Override public void contextInitialized(final ServletContextEvent sce) {
        final String httpsHeader = request.getHeader(LB_HTTPS_HEADER);
        boolean secure = httpsHeader != null && httpsHeader.equalsIgnoreCase("on");

        sce.getServletContext().getSessionCookieConfig().setSecure(secure);
    }   
}
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Vadim
  • 417
  • 3
  • 14
  • No, you can't get a request from the ServletContextEvent, and it's not meaningful to want to do that since it is _"the event class for notifications about changes to the servlet context of a web application"_. All the ServletContextEvent can give you is the [ServletContext](https://docs.oracle.com/javaee/7/api/javax/servlet/ServletContext.html), and you can't get the request from that either. See the accepted answer to [How can I get HttpServletRequest from ServletContext?](https://stackoverflow.com/q/31492792/2985643) for a full explanation. – skomisa Sep 06 '18 at 05:57
  • @skomisa Grand thanks. – Vadim Sep 06 '18 at 11:45

0 Answers0