在过滤器中修改HttpServletRequest 的cookies的sessionid

1.先写一个过滤器类,在过滤器中获得原有的request中的cookies,再new一个ModifyParametersWrapper对象,调用putHeader()去修改cookies中的sessionId   

public class SessionFilter extends Filter {
    public static final String SESSION = "SESSION";

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        ModifyParametersWrapper mParametersWrapper = new ModifyParametersWrapper(request);
        String sessionId = request.getHeader(SESSION);
        Cookie[] reqCookies = request.getCookies();
        StringBuilder cookies = new StringBuilder("");
        if (StringUtils.isNotBlank(sessionId)) {
            for (int i = 0; i < reqCookies.length; i++) {
                cookies.append(reqCookies[i].getName() + "=");
                if ("SESSION".equals(reqCookies[i].getName())) {
                    reqCookies[i].setValue(sessionId);
                    cookies.append(sessionId + ";");
                } else {
                    cookies.append(reqCookies[i].getValue() + ";");
                }
            }
            mParametersWrapper.putHeader("cookies", cookies.toString());
        }
        filterChain.doFilter(mParametersWrapper, response);
    }


    /**
     * 继承HttpServletRequestWrapper,创建装饰类,以达到修改HttpServletRequest参数的目的
     */
    private class ModifyParametersWrapper extends HttpServletRequestWrapper {
        private final Map customHeaders;

        ModifyParametersWrapper(HttpServletRequest request) {
            super(request);
            this.customHeaders = new HashMap<>();
        }

        void putHeader(String name, String value) {
            this.customHeaders.put(name, value);
        }

        public String getHeader(String name) {
            String headerValue = customHeaders.get(name);
            if (headerValue != null) {
                return headerValue;
            }
            return ((HttpServletRequest) getRequest()).getHeader(name);
        }

        public Enumeration getHeaderNames() {
            Set set = new HashSet<>(customHeaders.keySet());
            Enumeration e = ((HttpServletRequest) getRequest()).getHeaderNames();
            while (e.hasMoreElements()) {
                String n = e.nextElement();
                set.add(n);
            }
            return Collections.enumeration(set);
        }
    }
}

 

2.过滤器要在项目的Application.java启动类中注入过滤器Bean(在SpringBoot项目中写过滤器还可以通过在过滤器类名上加入@Order注解设置优先级,再写一个Config类,将下面代码放入其中)

@Bean
public FilterRegistrationBean sessionFilter() {
    FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
    SessionFilter sessionFilter = new SessionFilter();
    filterRegistrationBean.setFilter(sessionFilter);
    List urls = new ArrayList<>();
    urls.add("/*");//设置拦截路径
    filterRegistrationBean.setUrlPatterns(urls);//配置过滤规则
    filterRegistrationBean.setOrder(Integer.MIN_VALUE);
    return filterRegistrationBean;
}

另:想要修改request中的参数还可以通过类反射机制。参见https://blog.csdn.net/u010698072/article/details/79973830

你可能感兴趣的:(在过滤器中修改HttpServletRequest 的cookies的sessionid)