在tomcat中请求接收之前验证标头

时间:2015-12-12 05:57:16

标签: java web-services tomcat http-headers

在收到请求之前我需要验证标头。我发现tomcat阀门可以帮助它。我按照这些步骤但没有调用阀门:

  1. 制作一个maven项目并在其中执行此代码。

    package cz.ValveTest;
    
    import java.io.IOException;
    import java.util.Enumeration;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    
    import org.apache.catalina.connector.Request;
    import org.apache.catalina.connector.Response;
    import org.apache.catalina.valves.ValveBase;
    
    public class ProcessingValve  extends ValveBase {
    
    private static final Logger logger = Logger.getLogger(ProcessingValve.class.getName());
    
    @Override
    public void invoke(Request request, Response response) throws IOException,
            ServletException {
    
    
    HttpServletRequest httpServletRequest = request.getRequest();
    
     Enumeration<String> headerNames = httpServletRequest.getHeaderNames();
     logger.info("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
    
    while (headerNames.hasMoreElements()) { String header = headerNames.nextElement(); logger.log(Level.INFO, "Header --> {0} Value --> {1}", new Object[]{header, httpServletRequest.getHeader(header)}); }
      getNext().invoke(request, response);  
    
    } }
  2. 制作jar并将jar放入tomcat / lib文件夹

  3. 在server.xml中添加此行

    <valve className="cz.ValveTest.ProcessingValve"/>
    
  4. 重启tomcat。

  5. 现在我使用标题点击我的网络服务:

      

    期待:100-continue

  6. 但是在http命中没有调用这个配置和代码阀。如果有人知道为什么没有调用tomcat阀门,请帮忙。

1 个答案:

答案 0 :(得分:1)

server.xml中的代码区分大小写

所以试试这个:

<Valve className="cz.ValveTest.ProcessingValve"/>