Spring Boot向Filter过滤器中的Request对象添加额外的参数

有时候我们会遇到这么一些需求,在Filter中获取一些参数进行处理,同时将处理好的参数重新添加到Request对象中,这时候我们在Filter中直接使用request.setAttribute()是无效的。可以使用HttpServletRequestWrapper类来解决向Request添加额外参数的功能。

自定义HttpServletRequestWrapper类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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对象中去。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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,具体代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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();
}
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2021 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :