title: Spring: set User as parameter in "all" controllers and controller-functions (instead of Principal)
body: <p>In a simple Web Application I could retrieve a user (from session attributes) and set it as a parameter for all servlets, using a filter:</p>
  
  <p><strong>Inside Filter:</strong></p>
  
  <pre><code>@Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
      checkUser((HttpServletRequest) request);
      chain.doFilter(request, response);
  }
  
  private void checkUser(HttpServletRequest request) {
      HttpSession session = request.getSession(false);
      if (session != null) {
          request.setAttribute("user", session.getAttribute("user"));
      }
  }
  </code></pre>
  
  <p>Then I could use it in my JSP files (<code>&lt;c:if test="${not empty user}"&gt; blablabla</code>):</p>
  
  <pre><code>${user.getDisplayName}
  </code></pre>
  
  <hr>
  
  <p>In a Spring application I have to Inject principal in every Controller function. Even in that case a Principal is not a User, so I need to use the UserService every time:</p>
  
  <pre><code>@Controller
  @RequestMapping("/")
  public class IndexController {
  
      private final UserService userService;
  
      @Autowired
      IndexController(UserService userService) {
          this.userService = userService;
      }
  
      private static final String VIEW = "index";
  
      @RequestMapping(method = RequestMethod.GET)
      ModelAndView index(Principal principal) {
          User user = null;
          if (principal != null) {
              user = userService.findByUsername(principal.getName());
          }
          return new ModelAndView(VIEW, "user", user);
      }
  }
  </code></pre>
  
  <hr>
  
  <p>I need the user (entity class) object because it has different functions I use. E.g. <code>getDisplayName()</code> which I use for my navbar (on every page):</p>
  
  <p><strong>User class:</strong></p>
  
  <pre><code>public String getDisplayName() {
      if (firstname == null &amp;&amp; lastname == null) {
          return username;
      }
      if (firstname != null &amp;&amp; lastname != null) {
          return String.format("%s %s", firstname, lastname);
      }
      if (firstname != null) {
          return firstname;
      }
      return lastname;
  }
  </code></pre>
  
  <p>I cannot use this specific user-function (getDisplayName) in Spring Framework using Security tag library functions:</p>
  
  <pre><code>&lt;security:authentication property="name"/&gt; &lt;%-- So, this is not what I am looking for --%&gt;
  </code></pre>
  
  <hr>
  
  <p>Ok, I even tried to make a custom interceptor and register it (what I don't want to do, because I have to read my user every time from database again). But then I have problems with Autowiring the UserService inside an interceptor.</p>
  
  <p>What's the best way to solve this problem? Is it possible to find the User from database (findByUsername) once after a successful login, then put it in a session, then make some kind of interceptor that can do the same I did with filters before or retrieve user every time from database for every page but avoid this devious repeting code?</p>