1. 程式人生 > >利用過濾器修改request中的值

利用過濾器修改request中的值

1、構建request擴充套件類

package com.example.demo.wrapper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.HashMap;
import java.util.Map;

public class RequestWrapper extends HttpServletRequestWrapper {

    private Map<String, String[]> params = new HashMap<String, String[]>();

    public RequestWrapper(HttpServletRequest request) {
        super(request);
        //將現有parameter傳遞給params
        this.params.putAll(request.getParameterMap());
    }

    /**
     * 過載建構函式
     * @param request
     * @param extraParams
     */
    public RequestWrapper(HttpServletRequest request, Map<String, Object> extraParams) {
        this(request);
        addParameters(extraParams);
    }

    public void addParameters(Map<String, Object> extraParams) {
        for (Map.Entry<String, Object> entry : extraParams.entrySet()) {
            addParameter(entry.getKey(), entry.getValue());
        }
    }

    /**
     * 重寫getParameter,代表引數從當前類中的map獲取
     * @param name
     * @return
     */
    @Override
    public String getParameter(String name) {
        String[]values = params.get(name);
        if(values == null || values.length == 0) {
            return null;
        }
        return values[0];
    }

    /**
     * 同上
     * @param name
     * @return
     */
    @Override
    public String[] getParameterValues(String name) {
        return params.get(name);
    }

    /**
     * 新增引數
     * @param name
     * @param value
     */
    public void addParameter(String name, Object value) {
        if (value != null) {
            if (value instanceof String[]) {
                params.put(name, (String[]) value);
            } else if (value instanceof String) {
                params.put(name, new String[]{(String) value});
            } else {
                params.put(name, new String[]{String.valueOf(value)});
            }
        }
    }

}

2、建立過濾器,修改request中的值

package com.example.demo.filter;


import com.example.demo.wrapper.RequestWrapper;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public class RequestFilter implements Filter {

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

    }

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest)servletRequest;
        Enumeration<String> en = request.getParameterNames();
        Map<String, Object> extraParams = new HashMap<String, Object>();
        while (en.hasMoreElements()) {
            String key = en.nextElement();
            String[] arr = request.getParameterValues(key);
            String value = "";
            if (arr != null && arr.length > 0) {
                value = arr[0] + "_change";
            }
            System.out.println(key+"解密後:"+value);
            extraParams.put(key, value);
        }
        //利用原始的request物件建立自己擴充套件的request物件並新增自定義引數
        RequestWrapper requestParameterWrapper =new RequestWrapper(request);
        requestParameterWrapper.addParameters(extraParams);
        filterChain.doFilter(requestParameterWrapper, servletResponse);

    }
}

3、新增過濾器配置

package com.example.demo.config;

import com.example.demo.filter.RequestFilter;
import com.example.demo.filter.ResponseFilter;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;

import java.util.ArrayList;
import java.util.List;

@SpringBootConfiguration
public class ServletInitializer {

    @Bean
    public FilterRegistrationBean requestFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        RequestFilter requestFilter=new RequestFilter();
        filterRegistrationBean.setFilter(requestFilter);
        List<String> urls = new ArrayList<>();
        urls.add("/*");
        filterRegistrationBean.setUrlPatterns(urls);//配置過濾規則
        filterRegistrationBean.setOrder(1);
        return filterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean responseFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        ResponseFilter responseFilter=new ResponseFilter();
        filterRegistrationBean.setFilter(responseFilter);
        List<String> urls = new ArrayList<>();
        urls.add("/*");
        filterRegistrationBean.setUrlPatterns(urls);//配置過濾規則
        filterRegistrationBean.setOrder(2);
        return filterRegistrationBean;
    }

}