wysnylc
V2EX  ›  问与答

继承 MappingJackson2HttpMessageConverter 后重写 read()实现 json 解密(改写)

  •  
  •   wysnylc · Jul 27, 2016 · 7230 views
    This topic created in 3770 days ago, the information mentioned may be changed or developed.
    现在的问题是改写貌似有点问题,重写 writeInternal()进行 json 的加密倒是找到了 demo
    其实我不是要加密解密,我是要过滤 json 里面的 xss 。
    现在完成了 POST/GET 参数和 ResponseBody 的过滤,就差 RequestBody 的了,求指导,谢谢。
    10 replies  •  2019-03-01 14:08:39 +08:00
    wysnylc
    wysnylc
    OP
    Jul 27, 2016❤️1
    1
    谷歌都收录了但是没人回我呀宝宝好可怜
    wysnylc
    wysnylc
    OP
    Jun 20, 2017
    2
    我已经实现好了,你要么
    shiwuxiwen
    shiwuxiwen
    Jun 21, 2017
    3
    需要,这么发我的
    shiwuxiwen
    shiwuxiwen
    Jun 21, 2017
    4
    我 qq 767395949
    shiwuxiwen
    shiwuxiwen
    Jun 21, 2017
    5
    多谢,多谢。
    shiwuxiwen
    shiwuxiwen
    Jun 22, 2017
    6
    @wysnylc 有在不?
    wysnylc
    wysnylc
    OP
    Jun 23, 2017
    7
    @shiwuxiwen 两天没上,不要急
    package com.meadin.funding.service.support;

    import java.util.List;
    import java.util.Map;

    import javax.servlet.http.HttpServletRequest;

    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    import org.springframework.util.MultiValueMap;
    import org.springframework.web.servlet.handler.AbstractHandlerMapping;
    import org.springframework.web.util.HtmlUtils;
    import org.springframework.web.util.UrlPathHelper;

    import com.meadin.funding.util.CollectionUtils;

    /**
    * 解决 @PathVariable 注解造成的 xss 攻击问题,注意:此类必需由 WebApplicationContext 初始化
    *
    * @author zhou
    *
    */
    public class XssHandlerMappingPostProcessor implements BeanPostProcessor{

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException{
    return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException{
    if(bean instanceof AbstractHandlerMapping){
    AbstractHandlerMapping ahm = (AbstractHandlerMapping) bean;
    ahm.setUrlPathHelper(new XssUrlPathHelper());
    }

    return bean;
    }

    static class XssUrlPathHelper extends UrlPathHelper{

    @Override
    public Map<String, String> decodePathVariables(HttpServletRequest request, Map<String, String> vars){
    Map<String, String> result = super.decodePathVariables(request, vars);
    if(!CollectionUtils.isEmpty(result)){
    for(String key : result.keySet()){
    result.put(key, cleanXSS(result.get(key)));
    }
    }

    return result;
    }

    @Override
    public MultiValueMap<String, String> decodeMatrixVariables(HttpServletRequest request,
    MultiValueMap<String, String> vars){
    MultiValueMap<String, String> mvm = super.decodeMatrixVariables(request, vars);
    if(!CollectionUtils.isEmpty(mvm)){
    for(String key : mvm.keySet()){
    List<String> value = mvm.get(key);
    for(int i = 0; i < value.size(); i++){
    value.set(i, cleanXSS(value.get(i)));
    }
    }
    }

    return mvm;
    }

    private String cleanXSS(String value){
    return HtmlUtils.htmlEscape(value);
    }

    }

    }
    代码就是这样,应该没错
    18732907626
    18732907626
    Mar 1, 2019
    8
    @wysnylc 老哥在吗,需要你的帮助,我后台也是用的 @RequestBody User user 来接收前台传的值,现在要过滤 XSS,需要你的指点,方便的话可以加我 QQ334911558 感谢
    wysnylc
    wysnylc
    OP
    Mar 1, 2019❤️1
    9
    18732907626
    18732907626
    Mar 1, 2019
    10
    @wysnylc 还是不行,配置上了,没反应。什么情况
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2125 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 14:06 · PVG 22:06 · LAX 06:06 · JFK 09:06
    ♥ Do have faith in what you're doing.