CSRFController.java

  1. package edu.ucsb.cs156.example.controllers;

  2. import org.springframework.context.annotation.Profile;
  3. import org.springframework.security.web.csrf.CsrfToken;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RestController;

  6.  
  7. import io.swagger.v3.oas.annotations.Operation;
  8. import io.swagger.v3.oas.annotations.tags.Tag;

  9. /**
  10.  * The CSRF controller is used to get a CSRF token.
  11.  * This is only enabled in the development profile,
  12.  * and is used to test APIs with Postman or swagger.ui/
  13.  *
  14.  * For more information on CSRF, do a web search on "Cross-Site Request Forgery".
  15.  */

  16. @Profile("development")
  17. @Tag(name = "CSRF (enabled only in development; can be used with Postman to test APIs)")
  18. @RestController
  19. public class CSRFController {

  20.   /**
  21.    * This method returns a CSRF token.
  22.    * @param token the CSRF token, injected by Spring automatically
  23.    * @return the CSRF token
  24.    */
  25.   @Operation(summary= "Get a CSRF Token")
  26.   @GetMapping("/csrf")
  27.   public CsrfToken csrf(CsrfToken token) {
  28.     return token;
  29.   }
  30. }