title: OAuth2 userdetails not updating in spring security (SpringBoot)
body: <p>I am new to spring security and have used jhipster in which i have configured db and LDAP based authentications. Now i have integrated it with OAuth client using @enableOAuthSso. I can able to authenticate using external OAuth Idp (Okta) and it is redirecting to my application and my principle is getting updated and i can access resources through rest. But my userDetails object not getting populated.</p>
  
  <pre><code>@Inject
  public void configureGlobal(AuthenticationManagerBuilder auth) {
      try {
  
          auth
              .userDetailsService(userDetailsService)
              .passwordEncoder(passwordEncoder());
  
          auth
              .ldapAuthentication()
              .ldapAuthoritiesPopulator(ldapAuthoritiesPopulator)
              .userDnPatterns("uid={0},ou=people")
              .userDetailsContextMapper(ldapUserDetailsContextMapper)
              .contextSource(getLDAPContextSource());
  
      } catch (Exception e) {
          throw new BeanInitializationException("Security configuration failed", e);
      }
  }
  </code></pre>
  
  <p>I have check by going deep where its getting failed and found out the following</p>
  
  <pre><code>public static String getCurrentUserLogin() {
      SecurityContext securityContext = SecurityContextHolder.getContext();
      Authentication authentication = securityContext.getAuthentication();
      String userName = null;
      if (authentication != null) {
          log.info("authentication is not null");
          if (authentication.getPrincipal() instanceof UserDetails) { //failing here
              log.info("principle is instance of userdetails");
              UserDetails springSecurityUser = (UserDetails) authentication.getPrincipal();
              log.info(springSecurityUser.getUsername());
              userName = springSecurityUser.getUsername();
          } else if (authentication.getPrincipal() instanceof String) {
              userName = (String) authentication.getPrincipal();
          }
      }
      return userName;
  }
  </code></pre>
  
  <p>Its failing at the line </p>
  
  <pre><code>if(authentication.getPrincipal() instanceof UserDetails)
  </code></pre>
  
  <p>What is the possible and the best way to handle this to update user details object.</p>
  
  <p><strong>Update:</strong></p>
  
  <pre><code>@Transactional(readOnly = true)
  public User getUserWithAuthorities() {
      log.info("======inside getUserWithAuthorities =================");
      log.info("current user is :::::::"+SecurityUtils.getCurrentUserLogin());
      Optional&lt;User&gt; optionalUser = userRepository.findOneByLogin(SecurityUtils.getCurrentUserLogin());
      User user = null;
      if (optionalUser.isPresent()) {
          user = optionalUser.get();
          user.getAuthorities().size(); // eagerly load the association
      }
      return user;
  }
  </code></pre>
  
  <p>Its trying to fetch the user from db. But the user is not present in the database</p>