title: How to extend Spring oidcUser to use customUserDetails
body: <p>This is my first Question ever here on SO, it was helpfull and saved me lots of time, but now I can't find any solution to my problem.
  As I'm rather new to spring and espacially to spring-security, I'm stuck with something that might be easy if i had more knowledge.
  I have an existing Application that uses a local user database. It uses a custom UserDetails implementation that works if used with user:password authentification through a login form.
  Here is the current setup:</p>
  <pre><code>public class SecurityContext extends WebSecurityConfigurerAdapter {
  ....
    @Autowired
    public void configureGlobal(final AuthenticationManagerBuilder auth) throws Exception {
      auth.authenticationProvider(authenticationProvider()).userDetailsService(userDetailsService());
    }
  
    @Bean
    public AuthenticationProvider authenticationProvider() {
      DaoAuthenticationProvider result = new DaoAuthenticationProvider();
  
      result.setUserDetailsService(userDetailsService());
      result.setPasswordEncoder(passwordEncoder());
  
      return result;
    }
  
    @Override
    @Bean
    public GatesUserDetailsService userDetailsService() {
      GatesUserDetailsService result = new GatesUserDetailsService();
      result.setClientService(clientService);
      result.setAccountService(accountService);
      result.setCardService(cardService);
      result.setPersonService(personService);
      result.setAccountPropertyService(accountPropertyService);
      result.setLoginAttemptService(loginAttemptService);
  
      return result;
    }
  
  </code></pre>
  <p>Now I want to use SSO from an external IDP that speaks OpenIdConnect.
  Going through the documentation I was able to get this up and running in a &quot;default&quot; manner. That is, at the and of my process a get a user that is an Instance of OidcUser. I need that user to be either extended or incorporate the existing userDetails.
  The documentation (<a href="https://spring.io/guides/tutorials/spring-boot-oauth2/" rel="nofollow noreferrer">Spring Boot and OAuth2</a>) recommends to</p>
  <blockquote>
  <p>Implement and expose OAuth2UserService to call the Authorization
  Server as well as your database. Your implementation can delegate to
  the default implementation, which will do the heavy lifting of calling
  the Authorization Server. Your implementation should return something
  that extends your custom User object and implements OAuth2User.</p>
  </blockquote>
  <p>I was able to introduce my own Oauth2UserService that gets called right at the and of the authentification by setting:</p>
  <pre><code> @Override
    protected void configure(final HttpSecurity http) throws Exception {
      http.exceptionHandling()
          .accessDeniedHandler(accessDeniedHandler())
          .and()
          .oauth2Login()
          .failureHandler(authenticationFailureHandler())
          .successHandler(authenticationSuccessHandler())
          .userInfoEndpoint()
          .userService(this.oauth2UserService())
          .oidcUserService(this.oidcUserService());}
  
  private OAuth2UserService&lt;OidcUserRequest, OidcUser&gt; oidcUserService() {
      final OidcUserService delegate = new OidcUserService();
      return (userRequest) -&gt; { 
            OidcUser oidcUser = delegate.loadUser(userRequest);
             //..DO some additional Stuff check against external Server
           //Here I could load my custom userDetails
          GatesUserDetails userDetails = (GatesUserDetails) userDetailsService.loadUserByUsername(&quot;131:&quot; + username);
          ....
  </code></pre>
  <p>But I have now Idea how to make my customUser a vaild return to my function.
  I tried to implement the OidcUser Interface in my userDetails, but still it does not work.
  Any hint (even to a more understandable doc) would be highly appreciated.</p>
  <p><strong>EDIT</strong>
  To clarify things, I implemented the oidcUser Interface as stated in the docs along with the necessary implementations (getAttribute, getAttributes, getAuthorities) but still I could not use this as the return type would still be our GatesUserDetails, no way (for me) to cast it to oidcUser</p>