5

OAuth 2クライアントの資格フローで取得されたトークンを渡す必要がある一連のエンドポイントについて、 。私は、これらのエンドポイントを構築するために春ブーツと関連するプロジェクトを使用している、と私は、フレームワークは、以下のコードについては非常に独断ように見える理由として困惑している:Spring OAuthクライアントコンフィグレーションを読み込む方が簡単です

package com.example.client; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.security.oauth2.client.OAuth2ClientContext; 
import org.springframework.security.oauth2.client.OAuth2RestOperations; 
import org.springframework.security.oauth2.client.OAuth2RestTemplate; 
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails; 
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails; 
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RestController; 

@Configuration 
@EnableAutoConfiguration 
@EnableOAuth2Client 
@RestController 
public class StuffClient { 

    @Value("${security.oauth2.client.access-token-uri}") 
    private String tokenUrl; 

    @Value("${security.oauth2.client.id}") 
    private String clientId; 

    @Value("${security.oauth2.client.client-secret}") 
    private String clientSecret; 

    @Value("${security.oauth2.client.grant-type}") 
    private String grantType; 

    @Autowired 
    private OAuth2RestOperations restTemplate; 

    private String uri = "http://localhost:8082/stuff/"; 

    @RequestMapping(value = "/client/{stuffName}", method = RequestMethod.GET) 
    public String client(@PathVariable("stuffName") String stuffName) { 
     String request = uri + stuffName; 
     return restTemplate.getForObject(request, String.class); 
    } 

    @Bean 
    public OAuth2RestOperations restTemplate(OAuth2ClientContext clientContext) { 
     return new OAuth2RestTemplate(resource(), clientContext); 
    } 

    @Bean 
    protected OAuth2ProtectedResourceDetails resource() { 
     ClientCredentialsResourceDetails resource = new ClientCredentialsResourceDetails(); 
     resource.setAccessTokenUri(tokenUrl); 
     resource.setClientId(clientId); 
     resource.setClientSecret(clientSecret); 
     resource.setGrantType(grantType); 
     return resource; 
    } 
} 

とそれに付随する設定ファイル:

server: 
    port: 8081 

security: 
    basic: 
    enabled: false 
    oauth2: 
    client: 
     id: test-client 
     client-secret: test-secret 
     access-token-uri: http://localhost:8080/uaa/oauth/token 
     grant-type: client_credentials 

上記は正常に動作します。私は(JavaコードとYAMLの両方で)security.oauth2.client.client-idsecurity.oauth2.client.idを変更した場合、私は500エラーを取得し、最初の行は次のとおりです。

org.springframework.security.oauth2.client.resource.OAuth2AccessDeniedException: Unable to obtain a new access token for resource 'null'. The provider manager is not configured to support it. 

のすべてのためのIハードコード値ならばコードも正常に動作しますインスタンス変数security.oauth2.client.client-id

だから私の主な質問があるの値でclientIdを取り込むために、私は@Valueを使用するものを除いて、これらのインスタンス変数を取り込むのすべての順列で、実際には、正常に動作するようです:フレームワークは実際にこの非常に独断です特定の方法ですか?もしそうなら、なぜですか?そして、私のコードを単純化するために、この有名人を活用できますか?

答えて

0

使用しているスプリングブートバージョンがわかりません。私は春ブートバージョン1.5.4.RELEASEDを使用していますし、あなたのコードを簡素化するために、

あなたはOAuth2ProtectedResourceDetails

@Autowired 
private OAuth2ProtectedResourceDetails resource; 

などを注入し、 OAuth2RestTemplate

@Bean 
@Primary 
public OAuth2RestOperations restTemplate(OAuth2ClientContext clientContext) { 
    return new OAuth2RestTemplate(resource, clientContext); 
} 

としてサンプルのYAMLを作成することができます。..

### OAuth2 settings ### 
security: 
    user: 
    password: none 
    oauth2: 
    client: 
     accessTokenUri: ${auth-server}/oauth/token 
     userAuthorizationUri: ${auth-server}/oauth/authorize 
     clientId: myclient 
     clientSecret: secret 
    resource: 
     user-info-uri: ${auth-server}/sso/user 
     jwt: 
     keyValue: | 
      -----BEGIN PUBLIC KEY----- 
      your public key 
      -----END PUBLIC KEY----- 

次に、restTemplateインスタンスを使用しますコントローラ内には

​​

などがあります。

関連する問題