springBoot项目Filter中修改过滤器中的参数或者解密加密等操作

1.实现Filter接口
package com.perye.dokit.filters;

import com.perye.dokit.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebFilter(urlPatterns={"/app/*"},filterName="corsFilterParameter", asyncSupported = true)//urlPatterns过滤的哪些url
public class CorsFilterParameter implements Filter {
    private String filterName;
    @Autowired
    RedisUtils redisUtils;

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

    }
  @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                         FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse)servletResponse;

        HttpServletRequest request = (HttpServletRequest)servletRequest;
       String path = ((HttpServletRequest) request).getRequestURI();//请求路径


    }

    @Override
    public void destroy() {
    }

}
2.对request中所有参数值进行解密
package com.perye.dokit.filters;


import com.alibaba.fastjson.JSONObject;
import com.perye.dokit.utils.DES3Util;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.*;

@Slf4j
public class RequestWrapper extends HttpServletRequestWrapper {

    Map<String, String[]> params = null;

    public RequestWrapper(HttpServletRequest request, Map inParam) {
        super(request);
        params = new HashMap(inParam);
        getJSONParam(params);
    }

    public void setParameter(String key, String value) {
        params.put(key, new String[]{value});
    }

    public void setParameter(String key, String[] values) {
        params.put(key, values);
    }

    @Override
    public String getParameter(String name) {
        Object v = params.get(name);
        if (v == null) {
            return null;
        } else if (v instanceof String[]) {
            String[] strArr = (String[]) v;
            if (strArr.length > 0) {
                return strArr[0];
            } else {
                return null;
            }
        } else {
            return v.toString();
        }
    }

    @Override
    public Map<String, String[]> getParameterMap() {
        return params;
    }

    @Override
    public Enumeration<String> getParameterNames() {
        Vector l = new Vector(params.keySet());
        return l.elements();
    }

    @Override
    public String[] getParameterValues(String name) {
        return params.get(name);
    }
    public  void getJSONParam(Map<String, String[]> parameterMap){
//        Map<String, String[]> parameterMap = request.getParameterMap();
//        JSONObject returnObject = new JSONObject();
        for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
            String value = "";
            String[] values = entry.getValue();
            if (values != null){
                for (String s : values) {
                    value = s + ",";
                }
                value = value.substring(0, value.length() - 1);
            }
            value= DES3Util.DESDecrypt(value, "935fc32262a64692adcb126f16b1ebad");//解密

//            returnObject.put(entry.getKey(), value);
            setParameter(entry.getKey(),value);
        }

    }
}
3在启动类上注册该过滤器信息
@ServletComponentScan(basePackages = {"com.perye.dokit.filters"})
public class   DokitApplication {
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值