springspring-mvchttp-redirect

Cross domain post request from Spring controller


I need help for this particular case. The requirement is to redirect from spring controller to other domain website. We have some data to be posted as well. How can we make a post request (server-to-server) along with redirection?

For example: we got a request to http://example.com/employee/submit and we need to perform some operations on data here and then we need to redirect the user to different domain /process, this should be a post request and data needs to be posted to this URL.

Note: This can be achievable on the frontend side, but looking for some server side solution in Spring framework.

Thanks in advance.


Solution

  • You can try the following approach: handle the POST request from the client, collect the necessary data (key1, key2), send it as a server-to-server POST request to the remote server, then process the response, and finally return a 302 redirect to the client to navigate to another page:

    @RestController
    @RequestMapping("/employee")
    public class EmployeeController {
    
        @PostMapping("/submit")
        public ResponseEntity<?> submitEmployee() {
    
            // Collect data to send
            MultiValueMap<String, String> data = new LinkedMultiValueMap<>();
            data.add("key1", "value1");
            data.add("key2", "value2");
    
            // Set HTTP headers
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    
            // Prepare the request entity
            HttpEntity<MultiValueMap<String, String>> requestEntity = 
               new HttpEntity<>(data, headers);
    
            // Perform POST to the remote server (server-to-server)
            RestTemplate restTemplate = new RestTemplate();
            String remoteUrl = "https://otherdomain.com/process";
    
            ResponseEntity<String> response = 
               restTemplate.postForEntity(remoteUrl, requestEntity, String.class);
    
            // Optional: Process the response from the remote server
            if (response.getStatusCode() == HttpStatus.OK) {
                System.out.println("POST to remote server succeeded.");
            } else {
                System.out.println("POST failed with status: " + 
                    response.getStatusCode());
            }
    
            // Redirect the client to another page (GET)
            String redirectUrl = "https://otherdomain.com/result";
            HttpHeaders redirectHeaders = new HttpHeaders();
            redirectHeaders.setLocation(URI.create(redirectUrl));
    
        // 302 redirect
            return new ResponseEntity<>(
                   redirectHeaders, 
                   HttpStatus.FOUND
            ); 
        }
    }
    

    302 redirect is used to tell the browser to temporarily navigate to another URL using a GET request. This is appropriate here because the redirect should not be cached permanently (which would happen with a 301 redirect). Using 302 ensures that the client always follows the redirect dynamically.

    Note: This is not a cross-domain POST in the browser sense (CORS). Since the POST request is made from your Spring application to the remote server at the server level, CORS does not apply. CORS only affects requests made from a browser to a server, not between servers.