向filter過濾器中的request物件新增額外的引數
阿新 • • 發佈:2018-12-31
有時候我們會遇到這麼一些需求,在filter中獲取一些引數進行處理,同時將處理好的引數重新新增到request物件中,這時候我們在filter中直接使用request.setAttribute()是無效的。我們怎麼來解決這個問題呢,j2ee已經給我們提供瞭解決的辦法,使用HttpServletRequestWrapper類來解決向request新增額外引數的功能。
環境:spring boot
新建一個類
新建一個類,這個類繼承自HttpServletRequestWrapper
package com.spring.common.filter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.HashMap;
import java.util.Map;
/**
* com.hw.myp2c.common.filter
* Administrator
* 2017/10/18
**/
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());
}
/**
* 過載建構函式
* @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 (value != null) {
System.out.println(value);
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)});
}
}
}
}
通過建立這個類我們就能完成向request物件新增自己想要的引數了。
新增自定義引數
我們實現一個filter,這個filter實現一個功能,獲取使用者瀏覽器所訪問的域名,並且將使用者訪問的域名構建為一個新的引數新增到request物件中去。
package com.hw.myp2c.common.filter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* com.hw.myp2c.common.filter
* Administrator
* 2017/10/18
**/
public class RequestFilter implements Filter {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
// dn :域名
//
String dn = request.getServerName();
Map<String, Object> extraParams = new HashMap<String, Object>();
extraParams.put("dn", dn);
//利用原始的request物件建立自己擴充套件的request物件並新增自定義引數
RequestParameterWrapper requestParameterWrapper = new RequestParameterWrapper(request);
requestParameterWrapper.addParameters(extraParams);
filterChain.doFilter(requestParameterWrapper, servletResponse);
}
@Override
public void destroy() {
}
}
spring boot配置filter
我們在完成了新增自定義引數之後就需要在spring boot配置我們定義filter了,具體程式碼如下
package com.hw.myp2c;
import com.hw.myp2c.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();
}
}
這樣我們就實現了向request物件中新增我們自定義好了的引數了。