首先是简短的回答:)
您可以简单地添加标题
response.setHeader("P3P", "CP=\"CAO IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\"");
但是由于您需要所有资源的标题,因此最好使用过滤器
public class P3PFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
HttpServletResponse resp = (HttpServletResponse) res;
resp.addHeader("p3p", "CP=\"IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\"");
filterChain.doFilter(req, resp);
}
public void init(FilterConfig arg0) throws ServletException {
}
}
长答案
不久前我也遇到了同样的问题。可能就像您一样,我已经完成了我的功课,并且对 P3P 策略是什么以及它的使用方式有了一个公平的理解。我一次引用的是
官方链接
http://www.w3.org/P3P/
http://p3ptoolbox.com/guide/
著名博客
http://www.marco.org/2007/04/27/p3p-sucks-how-to-get-frame-cookies-unblocked-in-ie6
http://www.techrepublic.com/blog/software-engineer/craft-a-p3p-policy-to-make-ie-behave/
值得注意的 SO 问题
Cookie 被阻止/未保存在 Internet Explorer 的 IFRAME 中
P3P 政策无法在 IE 中允许 3rd 方 cookie
尽管如此,我仍然未能使其正常工作。我没有意识到,在这本神奇的书的帮助下我最终学到的是,引用
为了为 Internet Explorer 用户设置第三方 cookie(使用默认安全设置),您需要在您的资源中返回一个特殊的 P3P HTTP 标头,以声明您的服务打算如何使用用户数据。此标头需要与您的资源的所有HTTP 响应一起返回,而不仅仅是那些设置 cookie 的响应。这意味着静态资源、AJAX 端点、iframe——一切。
我怀疑这也可能是您的问题,我使用的 P3P 政策与您的几乎完全相同,因此您不会因无效政策而被拒绝。
如techrepublic博客中所述,我将没有 URL 的标头设置为 p3p 策略
IE没有将compact策略与full-format policy进行比较,full-format policy不需要
这在我的测试中被证明是正确的。这意味着您可以简单地将标题添加为
response.setHeader("P3P", "CP=\"CAO IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\"");
但是,由于您在所有响应中都需要它,因此最好编写一个过滤器,例如
public class P3PFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
HttpServletResponse resp = (HttpServletResponse) res;
resp.addHeader("p3p", "CP=\"IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\"");
filterChain.doFilter(req, resp);
}
public void init(FilterConfig arg0) throws ServletException {
}
}
并对所有请求应用过滤器。
<filter>
<filter-name>P3P Filter</filter-name>
<filter-class>your.package.P3PFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>P3P Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>