2 miaomiaorose MiaomiaoRose 于 2016.01.28 10:52 提问

springmvc拦截器拦截可以处理非法字符吗?

我拦截请求,并对请求的参数做了过滤 如图:

图片说明

然后到了controller里面 参数并没有改变 ,请问是我解决方法的问题吗?这种方法行的通吗?还是少了些东西?谢谢大神

图片说明

6个回答

wojiushiwo945you
wojiushiwo945you   Ds   Rxr 2016.01.28 11:30

你这个拦截器有没有配置呢,调试下这个拦截器的代码有没有执行呢?

MiaomiaoRose
MiaomiaoRose 已经进去了值也改了 就是到controller里时 取的又是原来的值
接近 2 年之前 回复
rui888
rui888   Ds   Rxr 2016.01.28 11:35
MiaomiaoRose
MiaomiaoRose 已经进去了值也改了 就是到controller里时 取的又是原来的值
接近 2 年之前 回复
MiaomiaoRose
MiaomiaoRose   2016.01.28 11:44

图片说明

我确定拦截器在controller之前已经进去了

rui888
rui888   Ds   Rxr 2016.01.28 11:48

建议在HttpServletRequestWrapper https://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/http/HttpServletRequestWrapper.html 中处理

http://stackoverflow.com/questions/27504696/how-to-change-the-posted-values-with-a-spring-mvc-interceptor

 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.
rui888
rui888   Ds   Rxr 2016.01.28 11:53

应该 用 OncePerRequestFilter 过滤在 http://blog.csdn.net/geloin/article/details/7445441

rui888
rui888   Ds   Rxr 2016.01.28 11:58

spring 拦截去可以修改request 内容吗?

rui888
rui888 回复MiaomiaoRose:我昨晚回去测试了,filter 中可以改变传入到controller 的参数
接近 2 年之前 回复
rui888
rui888 回复MiaomiaoRose: http://www.ocpsoft.org/opensource/how-to-safely-add-modify-servlet-request-parameter-values/
接近 2 年之前 回复
rui888
rui888 回复MiaomiaoRose: http://blog.csdn.net/xieyuooo/article/details/8447301
接近 2 年之前 回复
MiaomiaoRose
MiaomiaoRose 我又用了filter 并不能改变我的controller获取的参数值........................
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!