Cancel
Showing results for 
Search instead for 
Did you mean: 

Spring boot app page not getting posted.

Experimenter
Experimenter

I have created a simple spring boot application of two pages. From page one the request is posted to another page, I get an error. This works on my local machine but when I deploy the app to cloud foundry and post the first page, it shows me below error:

 

Error1.png

Below is the Controller class java file code:

 

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class GreetingController {

    @GetMapping("/greeting")
    public String greetingForm(Model model,HttpServletRequest request,@ModelAttribute Greeting greeting) 
    {
        model.addAttribute("greeting", new Greeting());
        Logger logger = LoggerFactory.getLogger(GreetingController.class);
        System.out.println("GET Controller");
    	logger.info("GET Controller");
        return "greeting";
    }
    
    @PostMapping("/greeting")
    public String greetingSubmit(@ModelAttribute Greeting greeting,HttpServletRequest request) 
    {
    	Logger logger = LoggerFactory.getLogger(GreetingController.class);
    	System.out.println("POST Controller");
    	logger.info("POST Controller");
        return "result";
    }
}

greeting.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Handling Form Submission</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
	<h1>Form</h1>
    <form action="#" th:action="@{/greeting}" th:object="${greeting}" method="post">
    	<p>Id: <input type="text" th:field="*{id}" /></p>
        <p>Message: <input type="text" th:field="*{content}" /></p>
        <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
    </form>
</body>
</html>

result.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Handling Form Submission</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
	<h1>Result</h1>
    <p th:text="'id: ' + ${greeting.id}" />
    <p th:text="'content: ' + ${greeting.content}" />
    <a href="/greeting">Submit another message</a>
</body>
</html>

Please share your views and help me on this.

 
 
2 REPLIES

Re: Spring boot app page not getting posted.

Legend
Legend

Your client code is not setting the required xsrf header, please read the link below. This affects specially POST requests:
https://developer.mindsphere.io/concepts/concept-authentication.html#calling-apis-from-frontend

If you use one of the existing web UI frameworks like Angular that is done *almost* automatically for you. In your case you seem to be using raw Java Spring Framework with Thymeleaf; I'd take a look at:
https://www.baeldung.com/spring-security-csrf

Most probably you just need to add the hidden form element with the xsrf token.

Re: Spring boot app page not getting posted.

Legend
Legend

This might also help:

https://www.baeldung.com/csrf-thymeleaf-with-spring-security

 

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