簡體   English   中英

在2.0版中具有自定義TokenGranter的Spring Security OAuth2 +

[英]Spring Security OAuth2 with custom TokenGranter in version 2.0.+

在OAuth2的早期版本中,可以通過將自定義令牌授予者添加到<authorization-server>元素的xml配置中來添加它。

我想知道如何在不丟失默認配置的情況下使用AuthorizationServerConfigurerAdapter使用Java Config擴展授權服務器,該默認配置包含隱式,客戶端憑據,刷新令牌和授權代碼授予類型。

第一次嘗試是使用@Component創建TokenGranter:

@Component("customTokenGranter")
public class CustomTokenGranter {
     //implementation
}

這導致依賴項解析異常,因為無法自動裝配構造授予者所需的tokenServices。

第二次嘗試是使用configure方法

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception
{
    endpoints
        .tokenGranter(new CustomTokenGranter(endpoints.getTokenServices(),
                endpoints.getClientDetailsService(), endpoints.getOAuth2RequestFactory()));

}

使用此選項,將不會注冊默認的授予類型。

我還嘗試了較低順序的第二種配置,但沒有成功。 我還能做些什么來添加我的自定義資助類型?

您還需要添加默認值,例如,使用CompositeTokenGranter

        List<TokenGranter> tokenGranters = getTokenGranters(); // implementation up to you
        tokenGranter = new CompositeTokenGranter(tokenGranters);
        endpoints.tokenGranter(tokenGranter);

這是另一種方式。 從這里復制。

在這個例子中,一個新的自定義TokenGranter ,名為CustomTokenGranter ,被添加到CompositeTokenGranter使用默認TokenGranters 我喜歡這個示例,因為它使用AuthorizationServerEndpointsConfigurer的公共方法getTokenGranter()來檢索默認的TokenGranter

@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<TokenGranter> granters = new ArrayList<TokenGranter>(Arrays.asList(endpoints.getTokenGranter()));
        granters.add(new CustomTokenGranter(endpoints.getTokenServices(), endpoints.getClientDetailsService(), endpoints.getOAuth2RequestFactory(), "custom"));
        return new CompositeTokenGranter(granters);
    }

由於找不到對ClientDetailService的依賴,因此很難從getTokenGranter方法獲取默認授予者,因此我找不到解決方法。 我復制了AuthorizationServerEndpointsConfigurer#tokenGranter()中的代碼,並將clientDetailService和其他bean直接傳遞給構造函數。 請注意,我添加了一個創建DefaultOAuth2RequestFactory的方法,以傳遞給授予者和端點:

public TokenGranter tokenGranter() {

            ClientDetailsService clientDetails = clientDetailsService;
            AuthorizationServerTokenServices tokenServices = tokenServices();
            AuthorizationCodeServices authorizationCodeServices = authorizationCodeServices();
            OAuth2RequestFactory requestFactory = requestFactory();

            List<TokenGranter> tokenGranters = new ArrayList<TokenGranter>();

            tokenGranters.add(new AuthorizationCodeTokenGranter(tokenServices, authorizationCodeServices,
                    clientDetails, requestFactory));
            tokenGranters.add(new RefreshTokenGranter(tokenServices, clientDetails, requestFactory));
            tokenGranters.add(new ImplicitTokenGranter(tokenServices, clientDetails, requestFactory));
            tokenGranters.add(new ClientCredentialsTokenGranter(tokenServices, clientDetails, requestFactory));
            tokenGranters.add(new ResourceOwnerPasswordTokenGranter(authenticationManager, tokenServices,
                        clientDetails, requestFactory));
            tokenGranters.add(new CustomTokenGranter(authenticationManager, tokenServices(), clientDetailsService,
                    requestFactory));

            return new CompositeTokenGranter(tokenGranters);
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

        endpoints
                .tokenServices(tokenServices())
                .tokenStore(tokenStore())
                .tokenEnhancer(tokenEnhancer())
                .authorizationCodeServices(authorizationCodeServices())
                .userApprovalHandler(userApprovalHandler())
                .authenticationManager(authenticationManager)
                .requestFactory(requestFactory())
                .tokenGranter(tokenGranter());
    }

話雖如此,我最終還是刪除了該代碼,只是添加了另一個AuthenticationProvider,因為我的新授予類型仍然使用UsernamePasswordAuthenticationToken的子類,這是默認情況下密碼授予所使用的身份驗證類型。

根據文檔,我們有:

贈款類型

可以通過AuthorizationServerEndpointsConfigurer配置AuthorizationEndpoint支持的授權類型。 默認情況下,除密碼外,所有授權類型均受支持(有關如何打開密碼的詳細信息,請參見下文)。 以下屬性影響授予類型:

authenticationManager:通過注入AuthenticationManager來打開密碼授予。 ......

請參閱文檔 因此,您可以像這樣注入AuthenticationManager:

    @Configuration
    @EnableAuthorizationServer
    public class AuthorizationServerConfigurer extends AuthorizationServerConfigurerAdapter {

        @Autowired
        private AuthenticationManager authenticationManager;

        @Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
            endpoints
                    .authenticationManager(authenticationManager)
........

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM