title: Accessing UserDetails object from controller in Spring using Spring security
body: <p>I would like to access some user details from session. I am using spring security and custom authentication by overriding <strong>loadUserByUsername(String username)</strong> method.</p>
  
  <p>I am returning a user and would like to access it from within <strong>my controller.</strong> I tried the principal object but i can not reach to the companyId field of my <strong>ESecurityUser</strong> object. </p>
  
  <p>Any help would be appreciated..</p>
  
  <pre><code>@Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
  
      ESecurityUser user = new ESecurityUser();
      user.setUsername("hello");
      user.setPassword("world");
      user.setCompanyId(199);
  
      Set&lt;EAuthority&gt; authorities = new HashSet&lt;EAuthority&gt;();
      EAuthority authority = new EAuthority();
      authority.setAuthority("ROLE_ADMIN");
      authorities.add(authority);
  
      user.setAuthorities(authorities);;
  
      return user;
  }
  </code></pre>
  
  <p>Sample Controller Code</p>
  
  <pre><code>@RequestMapping("")
      public String toPeriodicAdReport(@ModelAttribute("advertFormHelper") AdvertFormHelper advertFormHelper,
              Model model,Principal principal) {
  
          //What to write here so that i can access to authenticated user`s companyId field..
  
          return "Test";
      }
  </code></pre>