如何在localhost中处理安全cookie

4smxwvx5  于 2021-06-26  发布在  Java
关注(0)|答案(0)|浏览(244)

在我的应用程序中,我使用以下解决方案向会话头添加了安全标志:https://stackoverflow.com/a/16616225
我遇到了以下问题:
当我部署到远程机器时,它就工作了。我可以连接到它,没有问题!
它不适用于我的本地,因为http://localhost:7001不再有效,我需要使用https连接(https://localhost:7001).
问题是,我是否可以启用或知道我正在本地部署,并且我将使用http连接而不是https?比如写一个交换机案例,这样当我在本地部署它时,我就不会使用https,而当我部署到远程服务器时,我会使用https?

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("Set-Cookie", "JSESSIONID=" + ((HttpServletRequest)req).getSession().getId() + ";Path=/");

        HttpServletResponse response = (HttpServletResponse)res;

        chain.doFilter(req, response);
    }

    @Override
    public void destroy() {
    }
}
<filter>
        <filter-name>SecurityFilter</filter-name>
        <filter-class>package.SecurityFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>SecurityFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题