Post rendering method of 403 forbidden page instead of executing the mail method

note: I'm new to Play Framework

Using this video tutorial and playlist , I was able to create a simple webapp.

Problem:

POST methods in the routes file do not seem to execute the required POST code.

Given the routes file below, looking at localhost:{port}/user/register requires a GET , thus rendering and returning a register view .

Filling out the register view fields and clicking submit , refresh the page (clearing the input fields) and showing the expected "registered" text

If method="post" added to the form in the register view , an immediate 403 Forbidden page appears.

Why isnโ€™t the โ€œregisteredโ€ text displayed, what am I missing (something is wrong)?

Route File:

 GET / controllers.HomeController.index GET /user controllers.LoginController.index() GET /user/login controllers.LoginController.login() POST /user/login controllers.LoginController.doLogin() GET /user/register controllers.LoginController.register() POST /user/register controllers.LoginController.doRegister() # Map static resources from the /public folder to the /assets URL path GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset) 

Controllers

 HomeController LoginController 

LoginController methods:

 public Result index() { return ok(views.html.user.index.render(User.getAllUsers())) } public Result login() { return ok(login.render()); } public Result doLogin() { return ok("registered"); } public Result register() { return ok(register.render()); } public Result doRegister() { return ok("registered"); } 

Registration:

 @() <html> <head> <title>Register new User</title> </head> <body> <h1>Register User</h1> <br> <br> Enter Email Address: <input type="password" name="confirmPassword"> Enter Password: <input type="password" name="confirmPassword"> Confirm Password: <input type="password" name="confirmPassword"> <br> <br> <form action="@routes.LoginController.doRegister()"> <input type="submit" value="Register"/> </form> </body> </html> 

Console output error:

 [warn] p.filters.CSRF - [CSRF] Check failed because no or invalid token found in body [warn] p.filters.CSRF - [CSRF] Check failed with NoTokenInBody 
+1
source share
1 answer

I think that you have correctly defined the HTTP method (GET or POST) that will be used when submitting form data.

Perhaps you see Forbidden because "By default, Play will require a CSRF check" See here?

Add the CSRF token to the request as follows:

 @import helper._ <form method="post" action="@CSRF(routes.LoginController.doRegister())"> ... 

Or in the body of the form:

 <form method="post" action="@routes.LoginController.doRegister()"> @CSRF.formField ... 
+4
source

Source: https://habr.com/ru/post/1270509/


All Articles