mirror of
https://github.com/201206030/novel-plus.git
synced 2025-07-01 15:26:37 +00:00
29 lines
899 B
Java
29 lines
899 B
Java
package com.java2nb.common.xss;
|
|
|
|
import com.fasterxml.jackson.core.JsonGenerator;
|
|
import com.fasterxml.jackson.databind.JsonSerializer;
|
|
import com.fasterxml.jackson.databind.SerializerProvider;
|
|
import org.apache.commons.text.StringEscapeUtils;
|
|
|
|
import java.io.IOException;
|
|
|
|
public class XssStringJsonSerializer extends JsonSerializer<String> {
|
|
|
|
@Override
|
|
public Class<String> handledType() {
|
|
return String.class;
|
|
}
|
|
|
|
/**
|
|
* 假如有html代码是自己传来的,需要设定对应的name,不走StringEscapeUtils.escapeHtml4(value)过滤
|
|
*/
|
|
@Override
|
|
public void serialize(String value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
|
|
throws IOException {
|
|
if (value != null) {
|
|
String encodedValue = StringEscapeUtils.escapeHtml4(value);
|
|
jsonGenerator.writeString(encodedValue);
|
|
}
|
|
}
|
|
}
|