Spring Boot escape characters at Request Body for XSS protection

后端 未结 3 1643
别那么骄傲
别那么骄傲 2021-01-07 01:25

I\'m trying to secure my spring boot application using a XSSFilter like this:

public class XSSFilter implements Filter {

    @Override
    public void init(         


        
3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-07 02:16

    To remove XSS characters you just override AbstractJackson2HttpMessageConverter - this converter has responsibility to read request.inputStream to RequestBody object

    @Component
    public class XSSRequestBodyConverter extends AbstractJackson2HttpMessageConverter {
        public XSSRequestBodyConverter(ObjectMapper objectMapper) {
            super(objectMapper, MediaType.APPLICATION_JSON, new MediaType("application", "*+json"));
        }
    
    @Override
    public Object read(Type type, Class contextClass, HttpInputMessage inputMessage)
            throws IOException, HttpMessageNotReadableException {
    
        Object requestBody = super.read(type, contextClass, inputMessage);
        //Remove xss from requestBody here
        String requestInStr = objectMapper.writeValueAsString(requestBody);
        return objectMapper.readValue(replaceXSSCharacters(requestInStr), Object.class);
    }
    
    
    }
    

提交回复
热议问题