Cześć, mam problem z wygenerowaniem tokena dla usera który jest w bazie. Próbuje overridować metode retrieveUser lecz nie jest ona wywoływana.
Proszę o pomoc. Nie mam już pomysłu dlaczego to nie działa. Dla spring boot w wersji 1.x po małych korektach token jest generowany.

public class DaoAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {
// ~ Static fields/initializers
// =====================================================================================

/**
 * The plaintext password used to perform
 * PasswordEncoder#matches(CharSequence, String)}  on when the user is
 * not found to avoid SEC-2056.
 */
private static final String USER_NOT_FOUND_PASSWORD = "userNotFoundPassword";

// ~ Instance fields
// ================================================================================================

private PasswordEncoder passwordEncoder;

/**
 * The password used to perform
 * {@link PasswordEncoder#matches(CharSequence, String)} on when the user is
 * not found to avoid SEC-2056. This is necessary, because some
 * {@link PasswordEncoder} implementations will short circuit if the password is not
 * in a valid format.
 */
private volatile String userNotFoundEncodedPassword;

private CustomUserDetailsService userDetailsService;

private UserDetailsPasswordService userDetailsPasswordService;

public DaoAuthenticationProvider() {
    setPasswordEncoder(PasswordEncoderFactories.createDelegatingPasswordEncoder());
}

// ~ Methods
// ========================================================================================================

@SuppressWarnings("deprecation")
protected void additionalAuthenticationChecks(UserDetails userDetails,
                                              UsernamePasswordAuthenticationToken authentication)
        throws AuthenticationException {
    if (authentication.getCredentials() == null) {
        logger.debug("Authentication failed: no credentials provided");

        throw new BadCredentialsException(messages.getMessage(
                "AbstractUserDetailsAuthenticationProvider.badCredentials",
                "Bad credentials"));
    }

    String presentedPassword = authentication.getCredentials().toString();

    if (!passwordEncoder.matches(presentedPassword, userDetails.getPassword())) {
        logger.debug("Authentication failed: password does not match stored value");

        throw new BadCredentialsException(messages.getMessage(
                "AbstractUserDetailsAuthenticationProvider.badCredentials",
                "Bad credentials"));
    }
}

// protected void doAfterPropertiesSet() {
// Assert.notNull(this.userDetailsService, "A UserDetailsService must be set");
// }

@Override
protected final UserDetails retrieveUser(final String username,
                                         UsernamePasswordAuthenticationToken authentication)
        throws AuthenticationException {
    prepareTimingAttackProtection();
    try {
        UserDetails loadedUser = this.getUserDetailsService().loadUserByUsername(username);
        if (loadedUser == null) {
            throw new InternalAuthenticationServiceException(
                    "UserDetailsService returned null, which is an interface contract violation");
        }
        return loadedUser;
    }
    catch (UsernameNotFoundException ex) {
        mitigateAgainstTimingAttack(authentication);
        throw ex;
    }
    catch (InternalAuthenticationServiceException ex) {
        throw ex;
    }
    catch (Exception ex) {
        throw new InternalAuthenticationServiceException(ex.getMessage(), ex);
    }
}

@Override
protected Authentication createSuccessAuthentication(Object principal,
                                                     Authentication authentication, UserDetails user) {
    boolean upgradeEncoding = this.userDetailsPasswordService != null
            && this.passwordEncoder.upgradeEncoding(user.getPassword());
    if (upgradeEncoding) {
        String presentedPassword = authentication.getCredentials().toString();
        String newPassword = this.passwordEncoder.encode(presentedPassword);
        user = this.userDetailsPasswordService.updatePassword(user, newPassword);
    }
    return super.createSuccessAuthentication(principal, authentication, user);
}

private void prepareTimingAttackProtection() {
    if (this.userNotFoundEncodedPassword == null) {
        this.userNotFoundEncodedPassword = this.passwordEncoder.encode(USER_NOT_FOUND_PASSWORD);
    }
}

private void mitigateAgainstTimingAttack(UsernamePasswordAuthenticationToken authentication) {
    if (authentication.getCredentials() != null) {
        String presentedPassword = authentication.getCredentials().toString();
        this.passwordEncoder.matches(presentedPassword, this.userNotFoundEncodedPassword);
    }
}

/**
 * Sets the PasswordEncoder instance to be used to encode and validate passwords. If
 * not set, the password will be compared using {@link PasswordEncoderFactories#createDelegatingPasswordEncoder()}
 *
 * @param passwordEncoder must be an instance of one of the {@code PasswordEncoder}
 * types.
 */
public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
    Assert.notNull(passwordEncoder, "passwordEncoder cannot be null");
    this.passwordEncoder = passwordEncoder;
    this.userNotFoundEncodedPassword = null;
}

protected PasswordEncoder getPasswordEncoder() {
    return passwordEncoder;
}

public void setUserDetailsService(CustomUserDetailsService userDetailsService) {
    this.userDetailsService = userDetailsService;
}

protected UserDetailsService getUserDetailsService() {
    return userDetailsService;
}

public void setUserDetailsPasswordService(
        UserDetailsPasswordService userDetailsPasswordService) {
    this.userDetailsPasswordService = userDetailsPasswordService;
}

}

@Component
public class CustomUserDetailsService implements UserDetailsService {

@Autowired
private UserInfoService userRepository;

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

    UserInfo repositoryUser = userRepository.getUserInfoByUserName(username);
    if(null == repositoryUser) {
        throw new UsernameNotFoundException("user :"+username );
    }

    GrantedAuthority authority = new SimpleGrantedAuthority(repositoryUser.getRole());

    return new org.springframework.security.core.userdetails.User(username, repositoryUser.getPassword(),  Arrays.asList(authority));
}

}

@Configuration
@EnableAuthorizationServer
public class OAuthConfiguration extends AuthorizationServerConfigurerAdapter {

@Autowired
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;

@Autowired
CustomUserDetailsService userDetailsService;

@Override
public void configure(final AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
	oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()");
}

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
	clients.inMemory()
	.withClient("fooClientId").secret("$2y$12$6nzDzH9nWVbbcUYagcvdze7HRhUpTpVOLoL7Ksw8pyXml9yGJ.k1S")
	.authorizedGrantTypes("password", "authorization_code", "refresh_token").scopes("read","write")
	.authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT", "USER","ADMIN")
	.autoApprove(true)
	.accessTokenValiditySeconds(180)//Access token is only valid for 3 minutes.
    .refreshTokenValiditySeconds(600);//Refresh token is only valid for 10 minutes.;
}

@Override
public void configure(final AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
	endpoints.tokenStore(tokenStore()).authenticationManager(authenticationManager).accessTokenConverter(defaultAccessTokenConverter())
	.userDetailsService(userDetailsService);
}

@Bean
public TokenStore tokenStore(){
	return new JwtTokenStore(defaultAccessTokenConverter());
}

@Bean
public JwtAccessTokenConverter defaultAccessTokenConverter() {
	JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
	converter.setSigningKey("123");
	return converter;
}

}

@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
private static final String RESOURCE_ID = "resource-server-rest-api";
private static final String SECURED_READ_SCOPE = "#oauth2.hasScope('read')";
private static final String SECURED_WRITE_SCOPE = "#oauth2.hasScope('write')";
private static final String SECURED_PATTERN = "/secured/";
@Override
public void configure(ResourceServerSecurityConfigurer resources) {
resources.resourceId(RESOURCE_ID);
}
@Override
public void configure(HttpSecurity http) throws Exception {
/http.requestMatchers()
.antMatchers(SECURED_PATTERN).and().authorizeRequests()
.antMatchers(HttpMethod.POST, SECURED_PATTERN).access(SECURED_WRITE_SCOPE)
.anyRequest().access(SECURED_READ_SCOPE);
/
http.antMatcher("/
")
.authorizeRequests().anyRequest().authenticated();
}
}

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

@Autowired
CustomUserDetailsService userService;

@Bean
public DaoAuthenticationProvider daoAuthenticationProvider() {
	DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
	authenticationProvider.setPasswordEncoder(bCryptPasswordEncoder());
	authenticationProvider.setUserDetailsService(userService);
	System.out.println("Using my custom DaoAuthenticationProvider");
	return authenticationProvider;
}

@Override
protected void configure(HttpSecurity http) throws Exception {
	http.csrf().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().
	authorizeRequests().antMatchers(HttpMethod.GET,"/user").hasRole("ADMIN")
	.and().authorizeRequests().antMatchers("/oauth/token")
	.permitAll().anyRequest().authenticated();
}

@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
	return new BCryptPasswordEncoder();
}

@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
	return super.authenticationManagerBean();
}

@Autowired
public void configure(AuthenticationManagerBuilder auth) throws Exception {
	auth.authenticationProvider(daoAuthenticationProvider());
	auth.userDetailsService(userService);
}

}