IT/development

[spring Boot] Request method 'POST' not supported(feat. thymeleaf)

์•Œ ์ˆ˜ ์—†๋Š” ์‚ฌ์šฉ์ž 2022. 11. 22.

๋ชฉ์ฐจ

     

    ๋ฏธ๋ž˜์˜ ๋‚ด๊ฐ€ ๋ณด๊ธฐ ์œ„ํ•ด ๊ธฐ๋กํ•จ

    ์‚ฌ์ด๋“œ ํ”„๋กœ์ ํŠธ์—์„œ 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๊ฐ€ ์ผ์น˜ํ•˜๋Š”์ง€๋ถ€ํ„ฐ ํ™•์ธ ํ›„ ๋‹ค์Œ ์Šคํ…์„ ๋ฐŸ์•„์„œ ์—๋Ÿฌ๋ฅผ ์ฐพ์ž.

    ๋Œ“๊ธ€