title: How to generate Endpoint to get CSRF Token from Spring Boot?
body: <p>For Requests to a Spring Boot Application made with Kotlin I need to get the CSRF Token,
  to use them in subsequent requests against the Spring Boot Application.</p>
  <p>As I understand CSRF protection is enabled by default.</p>
  <pre><code>    fun getCurrentCsrfToken(): CsrfToken? {
      val attr = RequestContextHolder.currentRequestAttributes() as 
      ServletRequestAttributes
      val session = attr.request.getSession(false) ?: return null
  
      return session.getAttribute(&quot;org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository.CSRF_TOKEN&quot;) as CsrfToken
       }
  </code></pre>
  <p>But this returns null.</p>
  <p>Does someone know how to get the CSRF token?</p>
  <p>After I have a token I can provide it over an Endpoint.</p>
  <pre><code>    @GetMapping(&quot;/csrf&quot;)
      fun getPublicInfo(): ResponseEntity&lt;String&gt; {
      val token = getCurrentCsrfToken()
  
      return ResponseEntity.ok(token)
  }
  </code></pre>
  <p>I tried the following approach as well, but it does not work:</p>
  <pre><code>    @RequestMapping(value = [&quot;/csrf-token&quot;], method = [RequestMethod.GET])
      @ResponseBody
      fun getCsrfToken(request: HttpServletRequest): String? {
      val token = request.getAttribute(CsrfToken::class.java.name) as CsrfToken
      return token.token
  }
  </code></pre>