Just started to deal with Spring. I am trying to add data to the database and display it as a list on the page.

/** * Сущность **/ @Entity @Table(name = "que") public class Que { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String queName; private String queDescription; public Que(){} public Que(String queName, String queDescription) { this.queName = queName; this.queDescription = queDescription; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getQueName() { return queName; } public void setQueName(String queName) { this.queName = queName; } public String getQueDescription() { return queDescription; } public void setQueDescription(String queDescription) { this.queDescription = queDescription; } } /** * Репозиторий */ public interface QueRepo extends CrudRepository<Que, Long> { } /** * Контроллер */ @Controller public class QueController { @Autowired private QueRepo queRepo; @GetMapping("/que") public String createQue(Model model){ Iterable<Que> ques = queRepo.findAll(); model.addAttribute("ques", ques); return "createQue"; } @PostMapping("/que") public String createQue(@RequestParam String queName, @RequestParam String queDescription, Model model){ Que que = new Que(queName, queDescription); queRepo.save(que); Iterable<Que> ques = queRepo.findAll(); model.addAttribute("ques", ques); return "createQue"; } } <form method="post"> <input type="text" name="queName" class="form-control mt-3 col-sm-8 " placeholder="Name of que"> <input type="text" name="queDescription" class="form-control mt-3 col-sm-8" placeholder="Description"> <button class="btn btn-outline-info mt-3" formmethod="post" type="submit">Create</button> </form> <#list ques as que> <strong>${que.queName}</strong> <i>${que.queDescription}</i> </#list> 

Problem: Forbidden-403 error is issued, there are no more errors, I can not understand what is the matter and how to fix it?

1 answer 1

Just forgot to add csrf_token in the form of sending to the database. Those. The freemaker code should look like this:

 <form method="post"> <input type="hidden" name="_csrf" value="${_csrf.token}" /> <input type="text" name="queName" class="form-control mt-3 col-sm-8 " placeholder="Name of que"> <input type="text" name="queDescription" class="form-control mt-3 col-sm-8" placeholder="Description"> <button class="btn btn-outline-info mt-3" formmethod="post" type="submit">Create</button> </form> 

Do not forget csrf if you are using Spring Security