简体   繁体   English

如何确保响应中存在某些http标头

[英]how to ensure certain http headers are present in the response

I'm new to spring, so please excuse my ignorance. 我是春天的新手,所以请原谅我的无知。

I want to ensure that every response from my application contains the following headers: 我想确保来自应用程序的每个响应都包含以下标头:

Content-Type → application/json;charset=UTF-8
Date →Tue, 22 Nov 2016 16:30:21 GMT

is there a easy way of doing this, which I am missing? 有没有一种简单的方法可以做到? any advice or possible solutions would be great. 任何建议或可能的解决方案都将很好。 Thanks all :) 谢谢大家:)

I am not sure if that is going to work for you or not, because i cannot test it. 我不确定这是否对您有用,因为我无法对其进行测试。 the thing that I am sure of, is that you can do it, and the easiest way is : 我确定的事情是,您可以做到,最简单的方法是:

create a spring interceptor and override postHandle methode. 创建一个弹簧拦截器并覆盖postHandle方法。

try this out: 试试看:

Interceptor class : 拦截器类别:

package com.mkyong.common.interceptor; 软件包com.mkyong.common.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class MyInterceptor extends HandlerInterceptorAdapter{


    public void postHandle(
        HttpServletRequest request, HttpServletResponse response,
        Object handler, ModelAndView modelAndView)
        throws Exception {

        response.setHeader("Content-Type","application/json;charset=UTF-8");
    }
}

here is the list of header fields you can set, and dont forget to declare this class as spring bean. 这是您可以设置的头字段的列表,不要忘记将此类声明为spring bean。

<interceptors>
        <interceptor>
            <mapping path="/**" />
            <beans:bean class="package.MyInterceptor"></beans:bean>
        </interceptor>
</interceptors>

so, I solved this by creating a custom filter like so : 因此,我通过创建一个自定义过滤器来解决此问题,如下所示:

@Component
public class ResponseHeaderFilter implements Filter {
  @Override
  public void init(FilterConfig filterConfig) throws ServletException {

  }

  @Override
  public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
      throws IOException, ServletException {

    HttpServletResponse response = (HttpServletResponse) resp;

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
    String dateString = simpleDateFormat.format(UnixTime.now() * 1000);

    response.setHeader("Date", dateString);
    response.setHeader("Content-Type", "application/json;charset=UTF-8");

    chain.doFilter(req, resp);

  }

  @Override
  public void destroy() {

  }
}

this then created another problem with another custom filter in the code base. 然后在代码库中使用另一个自定义过滤器创建了另一个问题。 so I solve this additional problem with the help of this blog post: http://mtyurt.net/2015/07/15/spring-how-to-insert-a-filter-before-springsecurityfilterchain/ . 因此,我借助此博客文章解决了这个额外的问题: http : //mtyurt.net/2015/07/15/spring-how-to-insert-a-filter-before-springsecurityfilterchain/ Hope this helps someone :) 希望这可以帮助某人:)

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

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