1. 程式人生 > >filter過濾器,解決request獲取前端引數中,引數以json流的形式傳輸的問題

filter過濾器,解決request獲取前端引數中,引數以json流的形式傳輸的問題

1、複製流的工具

import jodd.io.StreamUtil;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by Administrator on 2018/1/12 0012.
 */
public class MAPIHttpServletRequestWrapper extends HttpServletRequestWrapper {
    private final byte[] body;

    /**
     * Constructs a request object wrapping the given request.
     *
     * @param request
     * @throws IllegalArgumentException if the request is null
     */
    public MAPIHttpServletRequestWrapper(HttpServletRequest request) throws IOException {
        super(request);
        body = StreamUtil.readBytes(request.getInputStream());
    }

    @Override
    public BufferedReader getReader() throws IOException {
        return new BufferedReader(new InputStreamReader(getInputStream()));
    }

    @Override
    public ServletInputStream getInputStream() {
        final ByteArrayInputStream bais = new ByteArrayInputStream(body);
        return new ServletInputStream() {
            @Override
            public boolean isFinished() {
                return false;
            }

            /**
             * Returns true if data can be read without blocking else returns
             * false.
             *
             * @return <code>true</code> if data can be obtained without blocking,
             * otherwise returns <code>false</code>.
             * @since Servlet 3.1
             */
            @Override
            public boolean isReady() {
                return false;
            }

            /**
             * Instructs the <code>ServletInputStream</code> to invoke the provided
             * {@link ReadListener} when it is possible to read
             *
             * @param readListener the {@link ReadListener} that should be notified
             *                     when it's possible to read.
             * @throws IllegalStateException if one of the following conditions is true
             *                               <ul>
             *                               <li>the associated request is neither upgraded nor the async started
             *                               <li>setReadListener is called more than once within the scope of the same request.
             *                               </ul>
             * @throws NullPointerException  if readListener is null
             * @since Servlet 3.1
             */
            @Override
            public void setReadListener(ReadListener readListener) {

            }

            @Override
            public int read() throws IOException {
                return bais.read();
            }
        };
    }
}
2、攔截器
import com.alibaba.fastjson.JSON;
import com.atc.beans.logger.LogParamVO;
import com.atc.beans.system.UserInfo;
import com.atc.framework.common.logger.LoggerService;
import com.atc.framework.common.utils.httprequest.MAPIHttpServletRequestWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by Administrator on 2018/1/12 0012.
 */
public class ParamFilter implements Filter {
    private static final Logger LOGGER = LoggerFactory.getLogger(ParamFilter.class);

    @Autowired
    private LoggerService loggerService;

    /**
     * @param filterConfig
     */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    /**
     * @param request
     * @param response
     * @param chain
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        ServletRequest servletRequest = loggerService.writeLog(request);
        chain.doFilter(servletRequest,response);
    }

    @Override
    public void destroy() {

    }

}

3、攔截器中需要執行的方法
import com.alibaba.fastjson.JSON;
import com.atc.beans.logger.LogParamVO;
import com.atc.beans.system.UserInfo;
import com.atc.framework.common.utils.httprequest.MAPIHttpServletRequestWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by Administrator on 2018/1/12 0012.
 */
@Service
public class LoggerServiceImpl implements LoggerService {
    private static final Logger LOGGER = LoggerFactory.getLogger(LoggerServiceImpl.class);
    @Override
    public ServletRequest writeLog(ServletRequest request) throws IOException {
        ServletRequest requestWrapper = null;
        if (request instanceof HttpServletRequest) {
            requestWrapper = new MAPIHttpServletRequestWrapper((HttpServletRequest) request);
        }

        String requestURL = ((HttpServletRequest) requestWrapper).getRequestURL().toString();

        //獲取json
        String contentType = requestWrapper.getContentType();
        String paramJson = "";
        if ("application/json; charset=UTF-8".equals(contentType)) {
            paramJson = this.getJsonParam((HttpServletRequest) requestWrapper);
        } else {
            paramJson = JSON.toJSONString(requestWrapper.getParameterMap());
        }
        LogParamVO logParamVO = new LogParamVO(yhxm, ip, requestURL, browser, paramJson);

        LOGGER.info("【Filter過濾使用者操作日誌】{}", JSON.toJSONString(logParamVO));

        if (requestWrapper == null) {
           return request;
        } else {
            return  requestWrapper;
        }
    }

    /**
     * 獲取Json資料
     *
     * @param request
     * @return
     */
    private String getJsonParam(HttpServletRequest request) {
        String jsonParam = "";
        ServletInputStream inputStream = null;
        try {
            int contentLength = request.getContentLength();
            if (!(contentLength < 0)) {
                byte[] buffer = new byte[contentLength];
                inputStream = request.getInputStream();
                for (int i = 0; i < contentLength; ) {
                    int len = inputStream.read(buffer, i, contentLength);
                    if (len == -1) {
                        break;
                    }
                    i += len;
                }
                jsonParam = new String(buffer, "utf-8");
            }
        } catch (IOException e) {
            LOGGER.error("引數轉換成json異常g{}", e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    LOGGER.error("引數轉換成json異常s{}", e);
                }
            }
        }
        return jsonParam;
    }


}