[Java / Kotlin] Escape (sanitize) HTML5 support with unbescape [Spring Boot]

things to do

Escape processing is performed when replacing the macro string of the HTML template with a parameter.

manner

This article uses ʻunbescape`.

Although the support for HTML5 is not specified, there is also a method using ʻApache Commons Text`.

Introduction

This library was in spring-boot-starter-thymeleaf, so if you have something related to SpringBoot, you may not need to do anything. For the time being, the repository of Maven is as follows.

Escape

I tried escaping with reference to the following article.

-HTML escape processing with JavaScript -Qiita

You can escape with HtmlEscape.escapeHtml5.

Verification code


fun main() {
    val value = """
        &: &
        ': '
        `: `
        ": "
        <: &lt;
        >: &gt;
    """.trimIndent()

    println(HtmlEscape.escapeHtml5(value))
}

Execution result

All but backticks have been escaped.

Execution result


&amp;: &amp;amp;
&apos;: &amp;#x27;
`: &amp;#x60;
&quot;: &amp;quot;
&lt;: &amp;lt;
&gt;: &amp;gt;

Other escapeable content

Below is a look at the package. You can escape in various ways.

スクリーンショット 2019-12-10 13.02.45.png

Articles that I used as a reference

-Tech Note-Try sanitizing HTML tags with java

Recommended Posts

[Java / Kotlin] Escape (sanitize) HTML5 support with unbescape [Spring Boot]
[Java] LINE integration with Spring Boot
Compare Hello, world! In Spring Boot with Java, Kotlin and Groovy
[Java] Hello World with Java 14 x Spring Boot 2.3 x JUnit 5 ~
[Java] Article to add validation with Spring Boot 2.3.1.
Download with Spring Boot
[Java] [Spring Boot] Specify runtime profile --Spring Boot starting with NetBeans
Generate barcode with Spring Boot
Hello World with Spring Boot
Java Config with Spring MVC
Implement GraphQL with Spring Boot
Handle Java 8 date and time API with Thymeleaf with Spring Boot
Get started with Spring boot
Hello World with Spring Boot!
Run LIFF with Spring Boot
SNS login with Spring Boot
[Java] Thymeleaf Basic (Spring Boot)
File upload with Spring Boot
Spring Boot starting with copy
CICS-Run Java application-(4) Spring Boot application
Using Mapper with Java (Spring)
Spring Boot starting with Docker
Hello World with Spring Boot
Set cookies with Spring Boot
Use Spring JDBC with Spring Boot
Add module with Spring Boot
Getting Started with Spring Boot
[Java] [Spring] Spring Boot 1.4-> 1.2 Downgrade Note
Create microservices with Spring Boot
Domain Driven Development with Java and Spring Boot ~ Layers and Modules ~
Send email with spring boot
HTML parsing with JAVA (scraping)
[Java] Sample project for developing web applications with Spring Boot
Spring Boot + Java + GitHub authentication login
Use Basic Authentication with Spring Boot
gRPC on Spring Boot with grpc-spring-boot-starter
Create an app with Spring Boot 2
Hot deploy with Spring Boot development
Database linkage with doma2 (Spring boot)
Elastic Beanstalk (Java) + Spring Boot + https
Spring Boot programming with VS Code
Until "Hello World" with Spring Boot
Inquiry application creation with Spring Boot
Java --Jersey Framework vs Spring Boot
Get validation results with Spring Boot
(Intellij) Hello World with Spring Boot
Create an app with Spring Boot
Google Cloud Platform with Spring Boot 2.0.0
Check date correlation with Spring Boot
Html5 development with Java using TeaVM
I tried GraphQL with Spring Boot
Beginning with Spring Boot 0. Use Spring CLI
I tried Flyway with Spring Boot
CORS support with Angular + Rest (Java)
Message cooperation started with Spring Boot
Spring Boot gradle build with Docker
A story about a Spring Boot project written in Java that supports Kotlin
[LINE BOT] I made a ramen BOT with Java (Maven) + Heroku + Spring Boot (1)
Introduction to Java development environment & Spring Boot application created with VS Code
Processing at application startup with Spring Boot
[Java] [Spring] Spring Boot Dependency injection mysterious hamarineta