Search code examples
javaspringcontrollerfreemarkerhttp-status-code-403

403 error with controller via FreeMarker and spring


I've created page *.ftl with some tags and want to all this work with my specific controller.

I found that code written inside <#list el as set> cant get controller

This works

<div>
    <form action="/deckStatusDel" method="post">
        <input type="text" name="deckstatusid" value="${deckstatus1}" />
        <input type="hidden" value="${deck.id}" name="deckId">
        <input type="hidden" name="_csrf" value="${_csrf.token}" />
        <button type="submit">Delete status</button>
    </form>
</div>

This doesn't

<#list deck_statuses as deck_status>
    <div>
        <form action="/deckStatusDel" method="post">
            <input type="text" value="${deck_status.id}" name="deckstatusid">
            <span>${deck_status.status}</span>
            <button type="submit">Delete status</button>
        </form>
    </div>
    <#else>
    empty
    </#list>

Controller code

@PostMapping("/deckStatusDel")
    public String deckStatusDelete(
            @RequestParam Long deckstatusid,
            @RequestParam("deckId") Deck deck
    ) {
        deckStatusesRepo.deleteById(deckstatusid);
        return "redirect:/deck/"+deck.getId();
    }

The code from the list is getting and sending the correct id, but i get 403 error. Why?


Solution

  • In the second form you are missing a csrf token for authentication

    <input type="hidden" name="_csrf" value="${_csrf.token}" />