• 使用java的自定义过滤器Filter 处理请求request 并响应response


    package com.enation.eop;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.util.Collection;
    import java.util.Date;
    import java.util.List;
    import java.util.Locale;
    
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.enation.eop.processor.back.BackendProcessor;
    import com.enation.eop.processor.core.freemarker.FreeMarkerPaser;
    import com.enation.eop.processor.facade.FacadeProcessor;
    import com.enation.eop.processor.facade.InstallProcessor;
    import com.enation.eop.processor.facade.ResourceProcessor;
    import com.enation.eop.resource.IAppManager;
    import com.enation.eop.resource.model.EopApp;
    import com.enation.eop.sdk.context.EopContext;
    import com.enation.eop.sdk.context.EopContextIniter;
    import com.enation.eop.sdk.context.EopSetting;
    import com.enation.framework.context.spring.SpringContextHolder;
    import com.enation.framework.context.webcontext.ThreadContextHolder;
    
    
    public class DispatcherDemoFilter implements Filter {
    
        public void init(FilterConfig config) {
    
        }
        
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            
            
    //        HttpServletRequest  req= ServletActionContext.getRequest();
    //        String  flag= req.getHeader("X-Requested-With");
    //            HttpServletResponse   response  = ServletActionContext.getResponse();
            String ip = ((HttpServletRequest) request).getHeader("x-forwarded-for");
            if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
                ip = ((HttpServletRequest) request).getHeader("Proxy-Client-IP");
            }
            if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
                ip = ((HttpServletRequest) request).getHeader("WL-Proxy-Client-IP");
            }
            if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
                ip = request.getRemoteAddr();
            }
            String authtoken = request.getParameter("auth-token");
            System.out.println(authtoken);
            StringBuffer sb = new StringBuffer() ; 
            InputStream is = request.getInputStream(); 
            InputStreamReader isr = new InputStreamReader(is);   
            BufferedReader br = new BufferedReader(isr); 
            String s = "" ; 
            while((s=br.readLine())!=null){ 
                sb.append(s) ; 
            } 
            System.out.println(sb);
            String str =sb.toString(); 
            System.out.println(ip+"时间"+new Date().getTime());
            //response.setCharacterEncoding("utf-8");
            response.setContentType("application/xml");
            String write = "<cart><items><item feed-id="449573" offer-id="15" price="825" count="1" delivery="true"/>" +
                    "</items><delivery-options><delivery service-name="Курьер" type="DELIVERY" price="1490.00">" +
                    "<dates from-date="24-10-2016" to-date="26-10-2016"/></delivery>" +
                    "</delivery-options><payment-methods>" +
                    "<payment-method>CASH_ON_DELIVERY</payment-method></payment-methods></cart>";
            System.out.println(write);
            response.getWriter().write(write);
        }
    
        public void destroy() {
    
        }
        
        private static boolean isExinclude(String uri) {
            String[] exts = new String[] { "jpg", "gif", "js", "png", "css", "doc", "xls", "swf","ico" }; 
            for (String ext : exts) {
                if (uri.toUpperCase().endsWith(ext.toUpperCase())) {
                    return true;
                }
            }
            return false;
        }
        
        
    }

    web.xml 配置增加

    <filter>
        <filter-name>dispatcherDemoFilter</filter-name>
        <filter-class>com.enation.eop.DispatcherDemoFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>dispatcherDemoFilter</filter-name>
        <url-pattern>/demo/*</url-pattern>
      </filter-mapping>
      <filter>

    这个配置的意思是过滤/demo/后面的所有请求

  • 相关阅读:
    计算机视觉
    深度学习2
    pytorch
    Django笔记
    深度学习
    numpy&pandas
    信息安全与密码技术
    Win10 alt+f4默认关机修改
    (转)Spring Boot 解决跨域问题的 3 种方案
    用 alibaba 的 fastjson 将 list 或 map 转为 json 字符串
  • 原文地址:https://www.cnblogs.com/jiafuwei/p/5994039.html
Copyright © 2020-2023  润新知