【问题标题】:Spring reject request that is missing vendor header (Custom MIME type)Spring 拒绝缺少供应商标头的请求(自定义 MIME 类型)
【发布时间】:2016-10-31 14:14:36
【问题描述】:

我有以下声明:

@RequestMapping(value= "/", method = RequestMethod.POST, headers = "Accept=application/vnd.woot4moo-v1+json"  

public @ResonseBody void create(String key){...}

这当然需要json,但我希望它拒绝任何不包含标头vnd.woot4moo-v1 部分的请求。这甚至可行吗?

【问题讨论】:

    标签: java spring header mime-types


    【解决方案1】:

    您可以通过实现HandlerInterceptor 来过滤特定请求,如下所示:

    RequestMethodInterceptor 类:

    public class RequestMethodInterceptor implements HandlerInterceptor  {
    
        @Override
        public boolean preHandle(HttpServletRequest request,
                HttpServletResponse response, Object handler) throws Exception {
            //Get the 'Accept' header value
            String headerValue = request.getHeader("Accept");
    
            //check the request contains expected header value
            if(!headerValue.equals("application/vnd.woot4moo-v1+json")) {
                //Reject and Log or Ignore upon your requirement & return false
                return false;
            } else {
                return true;
            }
        }
    }
    

    XML 配置:

    <mvc:interceptors>
          <bean class="xyz.RequestMethodInterceptor" />
        </mvc:interceptors>
    

    【讨论】:

      猜你喜欢
      • 2016-04-03
      • 2015-09-25
      • 2018-06-01
      • 1970-01-01
      • 2021-07-30
      • 2021-09-27
      • 2017-06-18
      • 2022-10-02
      • 2018-10-31
      相关资源
      最近更新 更多