title: spring-security-oauth2 2.0.7 refresh token UserDetailsService Configuration - UserDetailsService is required
body: <p>I would have one question regarding the configuration of spring-security-oauth2 2.0.7 please.
  I am doing the Authentication using LDAP via a GlobalAuthenticationConfigurerAdapter:</p>
  
  <pre><code>@SpringBootApplication
  @Controller
  @SessionAttributes("authorizationRequest")
  public class AuthorizationServer extends WebMvcConfigurerAdapter {
  
      public static void main(String[] args) {
          SpringApplication.run(AuthorizationServer.class, args);
      }
  
      @Override
      public void addViewControllers(ViewControllerRegistry registry) {
          registry.addViewController("/login").setViewName("login");
          registry.addViewController("/oauth/confirm_access").setViewName("authorize");
      }
  
      @Configuration
      public static class JwtConfiguration {
  
          @Bean
          public JwtAccessTokenConverter jwtAccessTokenConverter() {
              JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
              KeyPair keyPair = new KeyStoreKeyFactory(
                      new ClassPathResource("keystore.jks"), "foobar".toCharArray())
                      .getKeyPair("test");
              converter.setKeyPair(keyPair);
              return converter;
          }
  
          @Bean
          public JwtTokenStore jwtTokenStore(){
              return new JwtTokenStore(jwtAccessTokenConverter());
          }
      }
  
  
      @Configuration
      @EnableAuthorizationServer
      public static class OAuth2Config extends AuthorizationServerConfigurerAdapter implements EnvironmentAware {
  
          private static final String ENV_OAUTH = "authentication.oauth.";
          private static final String PROP_CLIENTID = "clientid";
          private static final String PROP_SECRET = "secret";
          private static final String PROP_TOKEN_VALIDITY_SECONDS = "tokenValidityInSeconds";
  
          private RelaxedPropertyResolver propertyResolver;
  
          @Inject
          private AuthenticationManager authenticationManager;
  
          @Inject
          private JwtAccessTokenConverter jwtAccessTokenConverter;
  
          @Inject
          private JwtTokenStore jwtTokenStore;
  
          @Inject
          private UserDetailsService userDetailsService;
  
          @Override
          public void setEnvironment(Environment environment) {
              this.propertyResolver = new RelaxedPropertyResolver(environment, ENV_OAUTH);
          }
  
          @Bean
          @Primary
          public DefaultTokenServices tokenServices() {
              DefaultTokenServices tokenServices = new DefaultTokenServices();
              tokenServices.setSupportRefreshToken(true);
              tokenServices.setTokenStore(jwtTokenStore);
              tokenServices.setAuthenticationManager(authenticationManager);
              return tokenServices;
          }
  
  
          @Override
          public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
              endpoints.authenticationManager(authenticationManager).tokenStore(jwtTokenStore).accessTokenConverter(
                      jwtAccessTokenConverter).userDetailsService(userDetailsService);
          }
  
          @Override
          public void configure(AuthorizationServerSecurityConfigurer oauthServer)
                  throws Exception {
              oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess(
                      "isAuthenticated()");
          }
  
          @Override
          public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
              clients.inMemory()
                      .withClient(propertyResolver.getProperty(PROP_CLIENTID))
                      .scopes("read", "write")
                      .authorities(AuthoritiesConstants.ADMIN, AuthoritiesConstants.USER)
                      .authorizedGrantTypes("authorization_code", "refresh_token", "password")
                      .secret(propertyResolver.getProperty(PROP_SECRET))
                      .accessTokenValiditySeconds(propertyResolver.getProperty(PROP_TOKEN_VALIDITY_SECONDS, Integer.class, 1800));
          }
      }
  
      @Configuration
      @Order(-10)
      protected static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  
          @Override
          protected void configure(HttpSecurity http) throws Exception {
              http
                      .formLogin().loginPage("/login").permitAll()
                      .and()
                      .requestMatchers().antMatchers("/login", "/oauth/authorize", "/oauth/confirm_access")
                      .and()
                      .authorizeRequests().anyRequest().authenticated();
          }
  
          @Bean
          @Override
          public AuthenticationManager authenticationManagerBean() throws Exception {
              return super.authenticationManagerBean();
          }
  
          @Bean
          @Override
          public UserDetailsService userDetailsServiceBean() throws Exception {
              return super.userDetailsServiceBean();
          }
      }
  
      @Configuration
      protected static class AuthenticationConfiguration extends
              GlobalAuthenticationConfigurerAdapter {
  
          @Override
          public void init(AuthenticationManagerBuilder auth) throws Exception {
              auth
                      .ldapAuthentication()
                      .userDnPatterns("uid={0},ou=people")
                      .groupSearchBase("ou=groups")
                      .contextSource().ldif("classpath:test-server.ldif");
          }
      }
  }
  </code></pre>
  
  <p>While the refresh token works fine with the release 2.0.6 of spring-security-oauth2, it does not work anymore with the version 2.0.7.
  As read <a href="https://github.com/royclarkson/spring-rest-service-oauth/issues/19" rel="noreferrer">here</a>, one should set the <code>AuthenticationManager</code> to be used when trying to get a new access token during the refresh.</p>
  
  <p>As far as I understand, this has something to do with the <a href="https://github.com/spring-projects/spring-security-oauth/commit/e303652a911cf0168b5be68383ac120c23ec63b3" rel="noreferrer">following</a> change of spring-security-oauth2.</p>
  
  <p>I unfortunately did not manage to set it up properly.</p>
  
  <pre><code>org.springframework.security.oauth2.provider.token.DefaultTokenServices#setAuthenticationManager
  </code></pre>
  
  <p>is called and gets an <code>AuthenticationManager</code> injected. I an not sure I understand how the <code>LdapUserDetailsService</code> is then going to be injected. The only thing I see is that the <code>PreAuthenticatedAuthenticationProvider</code> is going to be called while trying to re-authenticate the user during the token refresh call.</p>
  
  <p>Can anyone advise me on how to do it please?</p>
  
  <p>ps: The exception I am getting is the following:</p>
  
  <pre><code>p.PreAuthenticatedAuthenticationProvider : PreAuthenticated authentication request: org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken@5775: Principal: org.springframework.security.authentication.UsernamePasswordAuthenticationToken@441d5545: Principal: bob; Credentials: [PROTECTED]; Authenticated: true; Details: null; Granted Authorities: ROLE_USER; Credentials: [PROTECTED]; Authenticated: true; Details: null; Granted Authorities: ROLE_USER
  o.s.s.o.provider.endpoint.TokenEndpoint  : Handling error: IllegalStateException, UserDetailsService is required.
  </code></pre>