先简短回答:)
你可以简单地添加标题
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 blocked/not saved in IFRAME in Internet Explorer
P3P Policy not working to allow 3rd party cookies in IE
尽管如此,我仍然未能使其正常工作。我没有意识到,而我最终在amazing book 的帮助下学到的是,引用
为了为 Internet Explorer 用户设置第三方 cookie(使用
默认安全设置),您需要返回一个特殊的 P3P HTTP
标头与您的资源声明您的服务打算如何
使用用户数据。此标头需要使用 ALL HTTP 返回
对您的资源的响应,而不仅仅是那些设置 cookie 的资源。这
意味着静态资源、AJAX 端点、iframe——一切。
我怀疑这也可能是您的问题,我使用的 P3P 政策与您的几乎完全相同,因此您不会因无效政策而被拒绝。
如techrepublic 博客中所述,我将不带 URL 的标头设置为 p3p 策略
IE 不会将紧凑策略与完整格式策略进行比较,并且
不需要全格式策略
这在我的测试中被证明是正确的。这意味着您可以简单地将标题添加为
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>