XSS防御(@requestBody)


JSONではXSS防止のシーブレットフィルタで使用されるコードも適用される.
@Configuration
@ConditionalOnClass(name="com.fasterxml.jackson.databind.ObjectMapper")
protected static class JacksonCustomizerConfig {

	@Bean
	public Jackson2ObjectMapperBuilderCustomizer objectMapperBuilderCustomizer() {
		return customizer -> customizer.findModulesViaServiceLoader(true);
	}
}

@Configuration
@ConditionalOnClass(name="com.fasterxml.jackson.databind.module.SimpleModule")
public static class JacksonModuleProvider {

	@Bean
	public SimpleModule xssProtectionModule() {
		return new SimpleModule("XssProtectionModule", Version.unknownVersion(), ImmutableMap.of(String.class, new XssProtectionJsonDeserializer()));
	}
}

static class XssProtectionJsonDeserializer extends StringDeserializer implements ContextualDeserializer {

	private static final long serialVersionUID = 1L;

	@Override
	public JsonDeserializer<String> createContextual(final DeserializationContext c, final BeanProperty bp) {
		return this;
	}

	@Nullable @Override
	public String deserialize(final JsonParser p, final DeserializationContext c) throws IOException {
		return com.nhncorp.lucy.security.xss.XssPreventer.escape(super.deserialize(p, c));
	}
}