通过实现Filter与HttpServletRequestWrapper修改servlet请求中的getParameterMap参数
Posted Acmen-zym
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过实现Filter与HttpServletRequestWrapper修改servlet请求中的getParameterMap参数相关的知识,希望对你有一定的参考价值。
重写HttpServletRequestWrapper中ParameterMap相关的方法
public class CustomParameterRequestWrapper extends HttpServletRequestWrapper
private Map<String, String[]> params;
/**
* Constructs a request object wrapping the given request.
*
* @param request the @link HttpServletRequest to be wrapped.
* @throws IllegalArgumentException if the request is null
*/
public CustomParameterRequestWrapper(HttpServletRequest request, Map<String, String[]> params)
super(request);
this.params = params;
@Override
public Map<String, String[]> getParameterMap()
return params;
@Override
public Enumeration<String> getParameterNames()
Vector<String> l = new Vector<>(params.keySet());
return l.elements();
@Override
public String[] getParameterValues(String name)
Object v = params.get(name);
if (v == null)
return null;
else if (v instanceof String[])
return (String[]) v;
else if (v instanceof String)
return new String[](String) v;
else
return new String[]v.toString();
@Override
public String getParameter(String name)
Object v = params.get(name);
if (v == null)
return null;
else if (v instanceof String[])
String[] strArr = (String[]) v;
if (strArr.length > 0)
return strArr[0];
else
return null;
else if (v instanceof String)
return (String) v;
else
return v.toString();
实现Filter接口
@Component
@WebFilter(filterName = "customServletFilter", urlPatterns = "/*")
public class CustomServletFilter implements Filter
@Autowired
private AdminUserAuthRedis userAuthRedis;
@Override
public void init(FilterConfig filterConfig) throws ServletException
Filter.super.init(filterConfig);
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain chain) throws IOException, ServletException
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
String token = request.getHeader("token");
AdminUser adminUser = userAuthRedis.getUserLoginInfo(token);
if (adminUser == null)
chain.doFilter(request, response);
return;
//将请求中已存在的参数赋值到newParams中
Map<String, String[]> newParams = new HashMap<>(request.getParameterMap());
//设置用户相关信息
newParams.put("adminUserNick", new String[]adminUser.getAdminUserNick());
newParams.put("roleId", new String[]adminUser.getRoleId().toString());
newParams.put("adminUserId", new String[]adminUser.getAdminUserId().toString());
CustomParameterRequestWrapper wrapRequest = new CustomParameterRequestWrapper(request, newParams);
request.getParameterMap();
request = wrapRequest;
chain.doFilter(request, response);
@Override
public void destroy()
Filter.super.destroy();
效果展示
以上是关于通过实现Filter与HttpServletRequestWrapper修改servlet请求中的getParameterMap参数的主要内容,如果未能解决你的问题,请参考以下文章
JavaEE---过滤器Filter ,过滤器Filter的实现与部署,编码案例+权限案例
最纯粹的直播技术实战03-通过filter进行旋转及卡顿修复
最纯粹的直播技术实战03-通过filter进行旋转及卡顿修复