3

请教下大佬们, spring mvc 添加过滤器后 post 参数无法自动注入问题

 1 year ago
source link: https://www.v2ex.com/t/864848
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  Java

请教下大佬们, spring mvc 添加过滤器后 post 参数无法自动注入问题

  bingoshe · 3 小时 0 分钟前 · 257 次点击
添加了 xss 过滤后,因为读取了一次 body ,再次读取时无法读取,按照网上教程重写了 ServletInputStream ,可以手动重复读 body ,但是 controller 的 post 参数无法自动注入

第 1 条附言  ·  1 小时 56 分钟前

7 条回复    2022-07-08 11:18:56 +08:00

qinxi      2 小时 32 分钟前

参考 CommonsRequestLoggingFilter核心由 AbstractRequestLoggingFilter 提供

LeegoYih      2 小时 28 分钟前

用拦截器吧


新建一个类,实现`HandlerInterceptor`接口:

```java
public class FooHandlerInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// TODO
return true;
}

public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
// TODO
}

public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
// TODO
}
}
```

新建一个配置类,实现`WebMvcConfigurer`接口,重写`addInterceptors`方法,将拦截器注册到 Spring MVC 容器中:

```
@Configuration
public class FooConfiguration implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new FooHandlerInterceptor()).addPathPatterns("/**");
}
}
```


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK