title: Spring boot + thymeleaf: get logged in user
body: <p>I would like to know, how I can get the User object from Thymeleaf. Currently I am calling my userService, which will get the user from the DB. I don't like this approach, because for every call a db query will be made.</p>
  
  <p>Is it possible to get the user from memory?</p>
  
  <pre><code>&lt;link href="/css/style2.css"
      th:if="${@commanderService.getCurrentCommander()} and
          ${@commanderService.getCurrentCommander().settings == 'template=1'}" 
      rel="stylesheet" type="text/css" /&gt;
  </code></pre>
  
  <p>CommanderService:</p>
  
  <pre><code>public Commander getCurrentCommander() {
      Object principal = 
          SecurityContextHolder.getContext().getAuthentication().getPrincipal();
      Commander commander = findByName((String)principal);
  
      return commander;
  }
  </code></pre>