0

独自の oauth サーバーとリソース サーバーを実装するために Spring-Security-OAuth2 を使用しています。OAuth サーバーで (/oauth/check_token)を使用して任意の accessToken を認証する ResourceServer でRemoteTokenServiceas myを使用しています。ResourceServerTokenServiceCheckTokenEndpoint

/data/list などの API URL に antMatcher を追加しました。これには、クライアント アプリケーションの Role / Authority: " ROLE_ADMIN "が必要です。.antMatcher('/data/list').access("#oauth2.clientHasRole('ROLE_ADMIN')")

しかし、それは機能していません。このエンドポイントでいくつかの試行錯誤を行いましたが、得られるものは次のとおりです:::

oauth 付与がクライアントのみの場合、つまり client_credential 付与。/oauth/check_token から得られるもの

{
 "scope":["read"],
 "exp":1412955393,
 "client_id":"sample_test_client_app"
}

私たちはクライアント権限を取得しません。では、春のセキュリティはどのようにして上記の認証チェックを実行できますか"#oauth2.clientHasRole('ROLE_ADMIN')"

oauth 付与がユーザー + クライアントの場合、つまり Authorization_code は /oauth/check_token から取得したものを付与します

{
 "aud":["resource_id"],
 "exp":1412957540,
 "user_name":"developer",
 "authorities":["ROLE_USER"],
 "client_id":"sample_test_client_app",
 "scope":["read"]
}

そして、authorization_code grnat については、まだクライアントの権限/役割を取得していません。任意の API URL で clientHasRole 認証を実行する方法を教えてください。

4

1 に答える 1

3

「#oauth2.clientHasRole('ROLE_ADMIN')」が機能するには、AccessTokenConverter を実装し、それを認証サーバーとリソース サーバーに注入する必要があります。

したがって、メソッドを拡張DefaultAccessTokenConverterおよびオーバーライドする新しいクラスを作成します。convertAccessTokenextractAuthentication

メソッドでconvertAccessToken追加するだけです

Map<String, Object> response = (Map<String, Object>) super.convertAccessToken(token, authentication);
OAuth2Request clientToken = authentication.getOAuth2Request();
response.put("clientAuthorities", clientToken.getAuthorities());

そしてextractAuthenticationメソッドに追加

Collection<HashMap<String, String>> clientAuthorities = (Collection<HashMap<String, String>>) map.get("client_authority");

Collection<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();

for (HashMap<String, String> grantedAuthority : clientAuthorities) {
   for (String authority : grantedAuthority.values()) {
       grantedAuthorities.add(new SimpleGrantedAuthority(authority));
   }
}

Set<String> resourceIds = new LinkedHashSet<String>(map.containsKey(AUD) ? (Collection<String>) map.get(AUD) : Collections.<String> emptySet());
OAuth2Request request = new OAuth2Request(parameters, clientId, grantedAuthorities, true, scope, resourceIds, null, null, null);
  • 認証サーバーで:

    このクラスを設定しますAuthorizationServerEndpointsConfigurer

  • リソースサーバーで:

    このクラスを設定しますRemoteTokenServices

于 2014-10-11T10:09:07.643 に答える