본문 바로가기
Thymeleaf

[Thymeleaf] csrf 토큰 추가

by palbokdev 2023. 6. 19.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
    <head>
        <title>Please Log In</title>
    </head>
    <body>
        <h1>Please Log In</h1>
        <div th:if="${param.error}">
            Invalid username and password.</div>
        <div th:if="${param.logout}">
            You have been logged out.</div>
        <form th:action="@{/login}" method="post">
            <div>
            <input type="text" name="username" placeholder="Username"/>
            </div>
            <div>
            <input type="password" name="password" placeholder="Password"/>
            </div>
            <input type="submit" value="Log in" />
        </form>
    </body>
</html>

 

form에 th:action을 사용 시 csrf 토큰이 자동으로 추가됩니다.

 

ref : https://stackoverflow.com/questions/29509392/spring-boot-security-thymeleaf-and-csrf-token-not-injected-automatically