๋ชฉ์ฐจ

๋ฏธ๋์ ๋ด๊ฐ ๋ณด๊ธฐ ์ํด ๊ธฐ๋กํจ
์ฌ์ด๋ ํ๋ก์ ํธ์์ html template์ผ๋ก thymeleaf ์ฌ์ฉ ํ๊ณ ์๋ค.
๋ณดํต ์ด ์๋ฌ๋ ํด๋ผ์ด์ธํธ์ ์๋ฒ์ http method๊ฐ ์ผ์นํ์ง ์๋ ๊ฒฝ์ฐ ๋ฐ์ํ๋๋ฐ
๋ด ๊ฒฝ์ฐ๋ ์๋์ฒ๋ผ form์์๋ post๋ก ๋ณด๋ด๊ณ ์๋ฒ์์๋ post๋ก ๋ฐ๊ฒ๋ ๋์ด ์์๋ค.

view
<form role="form" action="/admin/board/new" th:object="${boardForm}" method="post">
<div class="form-group">
<label th:for="name">์ ๋ชฉ</label>
<input type="text" th:field="*{title}" class="form-control" placeholder="์ ๋ชฉ์ ์
๋ ฅํ์ธ์"
th:class="${#fields.hasErrors('name')}? 'form-control fieldError' : 'form-control'">
<p th:if="${#fields.hasErrors('title')}" th:errors="*{title}">Incorrect date</p>
</div>
<div class="form-group">
<label th:for="content">๋ด์ฉ</label>
<input type="text" th:field="*{content}" class="form-control" placeholder="๋ด์ฉ์ ์
๋ ฅํ์ธ์"
th:class="${#fields.hasErrors('content')}? 'form-control fieldError' : 'form-control'">
<p th:if="${#fields.hasErrors('content')}" th:errors="*{content}">Incorrect date</p>
</div>
<div class="form-group">
<label th:for="name">์ด๋ฆ</label>
<input type="text" th:field="*{name}" class="form-control" placeholder="์ด๋ฆ์ ์
๋ ฅํ์ธ์"
th:class="${#fields.hasErrors('name')}? 'form-control fieldError' : 'form-control'">
<p th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Incorrect date</p>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
Controller
/**
* ๊ฒ์๊ธ ์์ฑ ํผ
* @param model
* @return
*/
@GetMapping("/admin/board/new")
public String createForm(Model model) {
model.addAttribute("boardForm", new BoardVO());
return "board/createBoard";
}
/**
* ๊ฒ์๊ธ ์์ฑ ์ฒ๋ฆฌ
* @param boardVO
* @return
*/
@PostMapping("/admin/board/new")
public String create(BoardVO boardVO) {
boardService.insertBoard(boardVO);
return "redirect:/admin/board/list";
}
์..์๋ฌด ๋ฌธ์ ๊ฐ ์๋๋ฐ? ๐ฅ
๊ณฐ๊ณฐํ ์๊ฐํด ๋ณธ ๊ฒฐ๊ณผ ํ์ฌ spring security๊ฐ ์ ์ฉ๋์ด ์์๊ณ ์ด ๊ฒฝ์ฐ POST๋ก ๋ณด๋ผ ๋ csrf ํ ํฐ๊ฐ์ ๊ฐ์ด ๋ณด๋ด์ผ ๋๋ค๋ ๊ฑธ ๊น๋ฐ ํ๋ค.
์ด ๊ฒฝ์ฐ ํด๊ฒฐ ๋ฐฉ๋ฒ์ด 3๊ฐ์ง๊ฐ ์๋ค.
spring securiry ์ค์ ๋ณ๊ฒฝ
์ํํ๊ธด ํ๋ฐ spring security์์ csrf ํ ํฐ ์ฌ์ฉ ์ํจ์ผ๋ก ์๋์ฒ๋ผ ๋ณ๊ฒฝ

form submit ์ csrf ํ ํฐ ๊ฐ์ด ์ ์ก

thymeleaf์ผ ๊ฒฝ์ฐ th:action ์ฌ์ฉ

thymeleaf์ th:action์ ์ฌ์ฉํ ๊ฒฝ์ฐ csrf token ์ ์กํ๋ ๋ถ๋ถ์ด ํฌํจ๋์ด ์๋ค๊ณ ํจ
3๊ฐ์ง ์ค ํ 1ํด์ ์์ ํ๋ฉด ์ ์์ ์ผ๋ก ์์ฒญ๋์ด ์ฒ๋ฆฌ๊ฐ ๋๋ค.

Request method 'POST' not supported ์ด ์๋ฌ ๋ฉ์์ง์ ๊ฒฝ์ฐ ์์ธ์ด ๋ค์ํ๋ฏ๋ก
๋จผ์ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ์์ธ์ธ ํด๋ผ์ด์ธํธ์ ์๋ฒ์ http method๊ฐ ์ผ์นํ๋์ง๋ถํฐ ํ์ธ ํ ๋ค์ ์คํ ์ ๋ฐ์์ ์๋ฌ๋ฅผ ์ฐพ์.

'IT > development' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [JavaScript] Ajax ๊ฒฐ๊ณผ๊ฐ ๋ณ์ ์ ์ฅ (0) | 2022.11.22 |
|---|---|
| [JavaScript] submit control(feat. onsubmit) (0) | 2022.11.22 |
| [jQuery] jQuery.ajax() ์ฌ์ฉ๋ฒ ์์(feat. ๋๊ธ ํ์ด์ง๋ค์ด์ ) (0) | 2022.11.21 |
| [IDE] intellJ IDEA live template ์์ฑ (0) | 2022.11.21 |
| [mybatis] mybatis ์ด์ค foreach insert(feat. ์ด์ค ๊ณ์ธต ๊ฐ์ฒด ์ ์ฅ) (0) | 2022.11.21 |
๋๊ธ