初学springboot ,想自己定义一个 filter 截取参数用来验证签名,发现HttpServletRequest 并不能直接获取 json字符串,只能获取链接里的参数。
首先定义一个Filter文件, 然后实现Filter 的接口
package smaug.common.filters;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;
/**
* Created by naonao on 17/5/14.
*/
@WebFilter(filterName = "smaugFilter2", urlPatterns = "/*")
public class SmaugFilter extends BaseFilter implements Filter {
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 httpServletRequest = (HttpServletRequest) servletRequest;
//获取nginx http的真实IP
String realIp = httpServletRequest.getHeader("X-Real-IP");
// Map map = readJsonMap(httpServletRequest);
// logger.info("filter => " + servletRequest.getRemoteHost() + " " + JSONObject.toJSONString(map));
String quaryString = httpServletRequest.getQueryString();
logger.info("quaryString => " + quaryString);
String restUrl = (httpServletRequest.getRequestURL().toString());
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
}
}
然后将filter添加都请求链中
package smaug.common.configs;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import smaug.common.filters.SmaugFilter;
/**
* Created by naonao on 17/5/14.
*/
@Component
public class FilterConfig {
@Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(smaugFilter());
bean.setOrder(1);
bean.addUrlPatterns("/*");
return bean;
}
@Bean
public SmaugFilter smaugFilter() {
return new SmaugFilter();
}
}
因为article 方法是post 的,所以在postman 里 用post的方法 输入以下链接
http://localhost:3031/smaug/test/article?a=1&b=2
看到的log
2017-05-14 16:22:34.736 INFO 70532 --- [nio-3031-exec-1] smaug.common.filters.SmaugFilter : quaryString => a=1&b=2
哈哈,现在都是restful的 天下,这样的filter显然不能满足当初定下的目标。注释掉的代码有用处了哈哈
就是这行代码
Map<String, Object> map = readJsonMap(httpServletRequest);
SmaugFilter 继承了一个父类的BaseFilter
package smaug.common.filters;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.util.Map;
/**
* Created by naonao on 17/5/14.
*/
public class BaseFilter {
public String readJSONString(HttpServletRequest request) {
StringBuffer json = new StringBuffer();
String line = null;
try {
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null) {
json.append(line);
}
} catch (Exception e) {
System.out.println(e.toString());
}
return json.toString();
}
public Map readJsonMap(HttpServletRequest request) {
String json = readJSONString(request);
return JSONObject.parseObject(json, new TypeReference
此时postman的参数
"name" : "闹闹",
"age" : 12,
"requestId" : "12esdadfe3ed"
}
得到的log日志
{"requestId":"12esdadfe3ed","name":"闹闹","age":12}
此时可以针对不同的请求进行不同的预先操作,比如 权限验证