简体   繁体   English

在响应X-XSS-protection响应标头中读取标头的值

[英]reading the value of header in response X-XSS-protection response header

I am implementing a filter to set 我正在实施一个过滤器来设置

httpServletResponse.setHeader("X-XSS-Protection", "1; mode=block");

I have written the filter. 我已经写了过滤器。 I want to check if its working perfect or not. 我想检查它是否工作完美。

I thought to read the header from response object. 我想从响应对象中读取标头。 But I don't know how to do that. 但是我不知道该怎么做。

Can any one tell how to do it. 谁能告诉我该怎么做。

Or if there is abetter way of doing it, let me know. 或者,如果有更好的方法,请告诉我。

Edit 编辑

Updating the code 更新代码

public void doFilter(final ServletRequest req, final ServletResponse res, final FilterChain filterChain)
            throws IOException, ServletException 
            {   

        final HttpServletResponse response = (HttpServletResponse) res;
        final HttpServletRequest request = (HttpServletRequest) req;

        //set  X-XSS-protection in http header, other http headers can be added in same way
        String value = enable ? "1" : "0";  

        if(block) 
        {
            value += "; mode=block";
        }    

        PrintWriter out = response.getWriter();
        out.println("ready to set xss");
        response.setHeader("X-XSS-protection", value);
        out.println("<br/><br/>Xss has been set");          
        filterChain.doFilter(req, res);
        out.println("<br/><br/>XSS"+request.getHeader("X-XSS-protection"));     

        out.println("<br/><br/>job done");
    }

I am getting "XSSnull" 我收到“ XSSnull”

Please help me how do I correct it. 请帮助我如何纠正它。

Thanks in advance. 提前致谢。

You can get and read the response reader like this 您可以像这样获取和阅读响应阅读器

request.getHeader("name of the header");

I am guessing in your case you are aiming for something like this 我猜在你的情况下,你的目标是这样的

request.getHeader("X-XSS-Protection");

EDIT 编辑

for more clarification, you can think of a little analogy of Request and Response. 为了更清楚,您可以考虑一下“请求和响应”的类比。

  • Request - What you are sending. 请求-您要发送的内容。

  • Response - What you are receiving 回应-您收到什么

for more information about Request and Response please refer to this guide, if will containt all the information you need about Request and Response headers. 有关请求和响应的更多信息,请参阅指南,如果其中包含您需要的关于请求和响应头的所有信息。 Go specifically to 'Handling Http Response Headers' 专门转到'Handling Http Response Headers'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM