Spring Security OAuth2 with custom TokenGranter in version 2.0.+

前端 未结 4 912
无人及你
无人及你 2021-01-17 17:50

In previous versions of OAuth2 it was possible to add a custom token granter by adding it to the xml configuration in the element.<

4条回答
  •  轮回少年
    2021-01-17 18:30

    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);
        }
    

提交回复
热议问题