How do I sanitize string fields containing HTML at model level?
I have an app using Spring, JPA (Hibernate) and the Java validation framework (Hibernate Validator). I would like to be able to annotate fields in our domain model that are allowed to contain HTML and have them automatically sanitized at commit time. Anyone know a clever way to do this?
开发者_StackOverflow中文版I have tried using the validation framework but this does not support modifying the value of the field at validation time. I could hack things to get something working but am hoping for a cleaner solution.
You can do it with JPA Entity Listeners annotations:
@PreUpdate
@PrePersist
Anothers solution would be to do it in the own setter.
You can use the @SafeHtml
tag to do just that. You will need to include Jsoup
library as a dependency.
Heres some more info: https://www.baeldung.com/hibernate-validator-constraints
精彩评论