SpringCloud使用Filter过滤器实现@RequestParam参数加密

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringCloud使用Filter过滤器实现@RequestParam参数加密相关的知识,希望对你有一定的参考价值。

一、添加拦截器

@WebFilter中的filterName首字母一定要小写!!!

package com.xiaohang.socialcard.pre.filter;

import com.xiaohang.socialcard.pre.utils.SM4Util;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@WebFilter(urlPatterns = "/api/v1/get/getToken", "/api/v1/get/query", "/api/v1/get/page", filterName = "signAutheFilter")
public class SignAutheFilter implements Filter 
    @Override
    public void init(FilterConfig filterConfig) throws ServletException 

    

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException 

        HttpServletRequest request = (HttpServletRequest) req;

        String param = request.getParameter("param");
        try 
            param = SM4Util.encryptEcb("1234567890", param);
         catch (Exception e) 
            e.printStackTrace();
        
        MyRequestWrapper parameterRequestWrapper = new MyRequestWrapper(request);
        parameterRequestWrapper.addParameter("param", param);
        chain.doFilter(parameterRequestWrapper, res);// 替换原始请求参数

    

    @Override
    public void destroy() 

    


class MyRequestWrapper extends HttpServletRequestWrapper 

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

    @SuppressWarnings("unchecked")
    public MyRequestWrapper(HttpServletRequest request) 
        // 将request交给父类,以便于调用对应方法的时候,将其输出,其实父亲类的实现方式和第一种new的方式类似
        super(request);
        //将参数表,赋予给当前的Map以便于持有request中的参数
        this.params.putAll(request.getParameterMap());
    

    //重载一个构造方法
    public MyRequestWrapper(HttpServletRequest request, Map<String, Object> extendParams) 
        this(request);
        addAllParameters(extendParams);//这里将扩展参数写入参数表
    

    @Override
    public String getParameter(String name) //重写getParameter,代表参数从当前类中的map获取
        String[] values = params.get(name);
        if (values == null || values.length == 0) 
            return null;
        
        return values[0];
    

    public String[] getParameterValues(String name) //同上
        return params.get(name);
    

    public void addAllParameters(Map<String, Object> otherParams) //增加多个参数
        for (Map.Entry<String, Object> entry : otherParams.entrySet()) 
            addParameter(entry.getKey(), entry.getValue());
        
    

    public void addParameter(String name, Object value) //增加参数
        if (value != null) 
            if (value instanceof ArrayList) 
                String value1 = String.valueOf(value).substring(1, String.valueOf(value).length());
                value = value1.substring(0, value1.length() - 1);
                params.put(name, new String[](String) value);
             else 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));
            
        
    

二、Application.class这里一定要加上ServletComponentScan注解用于扫描Filter过滤器

package com.xiaohang.socialcard.pre;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@EnableFeignClients
@SpringBootApplication
@ServletComponentScan
public class Application 
    public static void main(String[] args) 
        SpringApplication.run(Application.class, args);
    

以上是关于SpringCloud使用Filter过滤器实现@RequestParam参数加密的主要内容,如果未能解决你的问题,请参考以下文章

springCloud学习05之api网关服务zuul过滤器filter

SpringCloud06_Gateway的概述工程案例代码微服务动态路由Predicate的使用详解Filter(建议收藏)

springcloud 网关过滤器Zuul Filter

SpringCloud - Spring Cloud 之 Gateway网关,Route路由,Predicate 断言,Filter 过滤器(十三)

SpringCloud - Spring Cloud Netflix 之 Zuul网关;过滤器filter

SpringCloud过滤filter