Spring Boot向Filter过滤器中的Request对象添加额外的参数
有时候我们会遇到这么一些需求,在Filter中获取一些参数进行处理,同时将处理好的参数重新添加到Request对象中,这时候我们在Filter中直接使用request.setAttribute()
是无效的。可以使用HttpServletRequestWrapper
类来解决向Request添加额外参数的功能。
自定义HttpServletRequestWrapper类
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.HashMap;
import java.util.Map;
/**
* @author yezhou
*/
public class RequestParameterWrapper extends HttpServletRequestWrapper {
private Map<String, String[]> params = new HashMap<String, String[]>();
public RequestParameterWrapper(HttpServletRequest request) {
super(request);
//将现有parameter传递给params
this.params.putAll(request.getParameterMap());
}
@Override
public Map<String, String[]> getParameterMap() {
return params;
}
/**
* 重载构造函数
* @param request
* @param extraParams
*/
public RequestParameterWrapper(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 (!params.containsKey(name) && 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)});
}
}
}
}
添加自定义参数
我们实现一个Filter,这个Filter实现一个功能,获取用户浏览器所访问的域名,并且将用户访问的域名构建为一个新的参数添加到Request对象中去。
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.keygen.Base64StringKeyGenerator;
import org.springframework.security.crypto.keygen.StringKeyGenerator;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
/**
* @author yezhou
*/
@Slf4j
public class RequestFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
log.info(httpRequest.getQueryString());
String dn = request.getServerName();
Map<String, Object> extraParams = new HashMap<String, Object>();
extraParams.put("dn", dn);
//利用原始的request对象创建自己扩展的request对象并添加自定义参数
RequestParameterWrapper requestParameterWrapper = new RequestParameterWrapper(httpRequest);
requestParameterWrapper.addParameters(extraParams);
filterChain.doFilter(requestParameterWrapper, servletResponse);
}
@Override
public void destroy() {
}
}
Spring Boot配置Filter
在完成添加自定义参数之后就需要在Spring Boot配置我们定义Filter,具体代码如下
import cn.appblog.mall.common.filter.RequestFilter;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.Filter;
@Configuration
@ServletComponentScan
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Myp2cMainApplication.class);
}
@Bean
public FilterRegistrationBean contextFilterRegistrationBean() {
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(requestFilter());
registrationBean.addUrlPatterns("/");
registrationBean.setName("requestFilter");
registrationBean.setOrder(1);
return registrationBean;
}
@Bean
public Filter requestFilter() {
return new RequestFilter();
}
}
版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/04/01/spring-boot-add-additional-parameters-to-request-in-filter/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。
THE END
0
二维码
打赏
海报
Spring Boot向Filter过滤器中的Request对象添加额外的参数
有时候我们会遇到这么一些需求,在Filter中获取一些参数进行处理,同时将处理好的参数重新添加到Request对象中,这时候我们在Filter中直接使用request.setAttr……
文章目录
关闭
共有 0 条评论