1

OAuth2 をサポートする Spring Boot アプリケーションが動作しています。2 つのクライアントがあり、それぞれに独自のクライアント ID があります。

@Configuration
@EnableAuthorizationServer
protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

    private static final String RESOURCE_ID = "my-rest-service";

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
               .withClient("clientOne")
               .authorizedGrantTypes("password", "refresh_token")
               .authorities("USER")
               .scopes("read", "write")
               .resourceIds(RESOURCE_ID)
               .secret("123456"))
               .and()
               .withClient("clientTwo")
               .authorizedGrantTypes("password", "refresh_token")
               .scopes("read","write")
               .resourceIds(RESOURCE_ID)
               .secret("789654")
               ;

    }
}

さらに、私は を持ってUserRoleます。ロール ROLE_ONE を持つユーザーは、使用時にのみ認証トークンを取得できclientOne、ROLE_TWO を持つユーザーのみがそれを取得するために使用できるようclientTwoになりました。

そのロジックをどこに追加すればよいですか?

4

1 に答える 1

0

私が今やったこと(しかし、他の/より良いオプションがあるかどうか聞きたいです)はResourceOwnerPasswordTokenGranter、メソッドからコードをコピーした場所のサブクラスを作成しgetOAuth2Authentication、カスタムロジックを追加することです:

...
if (userAuth == null || !userAuth.isAuthenticated()) {
    throw new InvalidGrantException("Could not authenticate user: " + username);
}

// Start of custom code
if( client.getClientId().equals("gamePlayClient")) {
  if( !((CustomUserDetails)userAuth.getPrincipal()).hasPlayerRole()) {
    throw new InsufficientAuthenticationException("Only users with the role 'PLAYER' can log on this client.");
  }
}
// End of custom code

OAuth2Request storedOAuth2Request = getRequestFactory().createOAuth2Request(client, tokenRequest);
return new OAuth2Authentication(storedOAuth2Request, userAuth);

これは、spring-security-oauth のバージョン 2.0.9.RELEASE を使用しています。

于 2016-06-22T12:39:12.653 に答える