2016-06-19 11 views
0

私は、ユーザーを認証するためにGoogle Single Sign On(SSO)を使用するSpring Bootアプリケーションを作成しようとしています。Spring BootとGoogle SSO

私はいくつかのチュートリアルを踏襲し、非常に基本的なセットアップを思い付いた:

appplication.properties

security.oauth2.client.client-id: xxx 
security.oauth2.client.client-secret: yyy 
security.oauth2.client.access-token-uri=https://www.googleapis.com/oauth2/v3/token 
security.oauth2.client.user-authorization-uri=https://accounts.google.com/o/oauth2/auth 
security.oauth2.client.client-authentication-scheme=query 
security.oauth2.client.scope=profile,email 
security.oauth2.resource.user-info-uri=https://www.googleapis.com/plus/v1/people/me 
security.oauth2.resource.prefer-token-info=false 

コントローラー

@RestController 
public class ExampleController { 

    @RequestMapping("/") 
    String hello(OAuth2Authentication authentication) { 
     return "Hello " + authentication.getName(); 
    } 

    @RequestMapping("/details") 
    Object details(OAuth2Authentication authentication) { 
     return authentication.getUserAuthentication(); 
    } 
} 

すべてがで正常に動作しますブラウザと私は私のGoogleの資格情報のためのプロンプトが表示され、その後、私は私のエンドポイントにアクセスできます。

問題は、このAPIにもプログラムでアクセスすることです(たとえば、cUrlまたはRestClient)。

curl xxx:[email protected]:8080/my-api/oauth/token -d grant_type=client_credentials 

をしかし、次の応答ました:

は、私は次のことを試してみました

{"timestamp":1466365089477,"status":403,"error":"Forbidden","message":"Expected CSRF token not found. Has your session expired?","path":"/my-api/oauth/token"} 

を私はプログラム的にSSO春ブーツアピスと連携する方法についていくつかの良いドキュメントやチュートリアルを見つけるのに苦労しています。誰かが私の欠けていることを説明したり、完全に機能するマルチユーザAPIの例を使って作業チュートリアルを教えてもらえますか?

答えて

1

Springブートの一部であるHosting an Authorization Server OAuth 2SocialApplication.javaの例を見てきましたか?

この例では、@EnableAuthorizationServerアノテーションを使用してOAuthトークンを付与できるサーバーを構成しています。リンクについて

$ curl acme:[email protected]:8080/oauth/token -d grant_type=client_credentials 
{"access_token":"370592fd-b9f8-452d-816a-4fd5c6b4b8a6","token_type":"bearer","expires_in":43199,"scope":"read write"} 

$ curl acme:[email protected]:8080/oauth/token -d grant_type=password -d username=user -d password=... 
{"access_token":"aa49e025-c4fe-4892-86af-15af2e6b72a2","token_type":"bearer","refresh_token":"97a9f978-7aad-4af7-9329-78ff2ce9962d","expires_in":43199,"scope":"read write"} 
+0

ありがとう:

は、クライアントがアクセストークンを要求できる方法を示す2つのcurlの例があります。後でそれをテストしますが、それは有望に見えます! – Smajl

+0

動作しているようですが、私はまだ私の残りのクライアントの要求(cUrlまたは同様のユーティリティ)をAPIに接続するために書く方法を理解できません... – Smajl