In previous versions of OAuth2 it was possible to add a custom token granter by adding it to the xml configuration in the
element.<
Here is another way. Copied from here.
In this example, a new custom TokenGranter
, named CustomTokenGranter
, is added to a CompositeTokenGranter
with the default TokenGranters
. I like this example because it uses the AuthorizationServerEndpointsConfigurer
's public method getTokenGranter()
to retrieve the default TokenGranter
's.
@Configuration
@EnableAuthorizationServer
protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager);
endpoints.tokenGranter(tokenGranter(endpoints));
}
private TokenGranter tokenGranter(final AuthorizationServerEndpointsConfigurer endpoints) {
List granters = new ArrayList(Arrays.asList(endpoints.getTokenGranter()));
granters.add(new CustomTokenGranter(endpoints.getTokenServices(), endpoints.getClientDetailsService(), endpoints.getOAuth2RequestFactory(), "custom"));
return new CompositeTokenGranter(granters);
}