问题描述
- springmvc拦截器拦截可以处理非法字符吗?
-
我拦截请求,并对请求的参数做了过滤 如图:然后到了controller里面 参数并没有改变 ,请问是我解决方法的问题吗?这种方法行的通吗?还是少了些东西?谢谢大神
解决方案
你这个拦截器有没有配置呢,调试下这个拦截器的代码有没有执行呢?
解决方案二:
你的拦截器进去了没啊?参考 http://examples.javacodegeeks.com/enterprise-java/spring/mvc/spring-mvc-handler-interceptors-example/
我确定拦截器在controller之前已经进去了
解决方案四:
建议在HttpServletRequestWrapper https://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/http/HttpServletRequestWrapper.html 中处理
I answer my own question. In fact it is rather complex and it took me some time to find a working solution. First, I created a filter in a Spring configuration class (Spring Boot environment exactly) :
@Configuration
@ComponentScan({ "istia.st.springmvc.config", "istia.st.springmvc.controllers", "istia.st.springmvc.models" })
@EnableAutoConfiguration
public class Config extends WebMvcConfigurerAdapter {
@Bean
public Filter cultureFilter() {
return new CultureFilter();
}
}
Here we declare a filter that will (by default) filter every request before it attains the final handler. Then I created the filter :
public class CultureFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
// next handler
filterChain.doFilter(new CultureRequestWrapper(request), response);
}
}
[OncePerRequestFilter] is a Spring class. The trick is to replace the actual request with a new one [CultureRequestWrapper(request)]. Then I created the CultureRequestWrapper :
public class CultureRequestWrapper extends HttpServletRequestWrapper {
public CultureRequestWrapper(HttpServletRequest request) {
super(request);
}
@Override
public String[] getParameterValues(String name) {
// posted values a et b
if (name != null && (name.equals("a") || name.equals("b"))) {
String[] values = super.getParameterValues(name);
String[] newValues = values.clone();
newValues[0] = newValues[0].replace(",", ".");
return newValues;
}
// other cases
return super.getParameterValues(name);
}
}
I redefined the [getParameterValues] of [HttpServletRequest] but it depends on the final servlet that will manage the request. We have to redefine the [HttpServletRequest] methods used by this servlet.
解决方案五:
应该 用 OncePerRequestFilter 过滤在 http://blog.csdn.net/geloin/article/details/7445441
解决方案六:
spring 拦截去可以修改request 内容吗?
时间: 2024-10-02 22:49:44