title: How to handle secure cookie in localhost
body: <p>In my application, I added secure flags to the session header using this solution: <a href="https://stackoverflow.com/a/16616225">https://stackoverflow.com/a/16616225</a></p>
  <p>I experienced the following problem:</p>
  <ul>
  <li>It works when I deploy to the remote machine. I can connect to it, no problems!</li>
  <li>It does not work on my local, because http://localhost:7001 is no longer valid, I need to use HTTPS connection (https://localhost:7001).</li>
  </ul>
  <p>The question is that can I enable or know that I am deploying on my local and I will use HTTP connection instead of HTTPS? Like writing a switch case so that when I deploy it locally, I won't use HTTPS and when I deploy to remote server, I will use HTTPS?</p>
  <pre class="lang-java prettyprint-override"><code>public class SecurityFilter implements Filter {
  
      @Override
      public void init(FilterConfig filterConfig) throws ServletException {
      }
  
      @Override
      public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
  
          // wrap the response
          HttpServletResponse response = new SecureCookieSetter((HttpServletResponse)res);
  
          // touch the session, so that it is added to the response header
          ((HttpServletRequest)req).getSession();
  
          response.setHeader(&quot;Set-Cookie&quot;, &quot;JSESSIONID=&quot; + ((HttpServletRequest)req).getSession().getId() + &quot;;Path=/&quot;);
          
          HttpServletResponse response = (HttpServletResponse)res;
  
          chain.doFilter(req, response);
      }
  
      @Override
      public void destroy() {
      }
  }
  
  </code></pre>
  <pre class="lang-xml prettyprint-override"><code>    &lt;filter&gt;
          &lt;filter-name&gt;SecurityFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;package.SecurityFilter&lt;/filter-class&gt;
      &lt;/filter&gt;
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;SecurityFilter&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  </code></pre>