2016-05-25 2 views
0

私はOAuth2で保護したいAPIを持っています。私はすでにgrant_typeというパスワードでダミーのテストを行いました。私は、トークンを要求し、トークンで保護されたエンドポイントにアクセスします。サーバーは、許可およびリソースサーバーとして機能します。暗黙のクライアントとcsrf保護を持つOAuth2

後で私はクライアントがjavascriptアプリケーションであるため暗黙的なgrant_typeを使用するべきだと読んだ。

私のクライアントは、そのように構成されています。私はこのようなエンドポイントにアクセスしようとした場合

@Override 
public void configure(final ClientDetailsServiceConfigurer clients) throws Exception {// @formatter:off 
clients 
    .inMemory().withClient("web") 
    .redirectUris("http://localhost:3000") 
    .secret("secret") 
    .authorizedGrantTypes("implicit", "refresh_token").scopes("read", "write") 
    .accessTokenValiditySeconds(3600).refreshTokenValiditySeconds(2592000); 
} 

:私はCSRFを持つことができますどのように

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

http://localhost:8080/oauth/authorize?grant_type=implicit&client_id=web&response_type=token&redirect_uri=http%3A%2F%2Flocalhost%3A3000

を、私はこれを取得APIを初めて呼び出す場合はトークンですか? (単にテスト用)であれば、私は私がこれを取得、その後CSRF無効:

{ 
    "timestamp": 1464136840865, 
    "status": 403, 
    "error": "Forbidden", 
    "exception": "org.springframework.security.authentication.InsufficientAuthenticationException", 
    "message": "Access Denied", 
    "path": "/oauth/authorize" 
} 

私はこの電話をかけることができるよとすべての作品パスワードgrant_typeとクライアントの設定: http://localhost:8080/oauth/token?grant_type=password&username=test&password=123 と認可の基本ヘッダを付加しますクライアントID /シークレットを使用します。

これを明確にするために、この独自の信頼できるクライアントを用意することが考えられます。だから、ユーザは、アプリケーションにアクセス権を与えるようにユーザに要求せずにログイン/パスワードを入力するだけでよい。

申し訳ありませんが、これは間違った質問です。私は私が見つけることができるすべてを読んできましたが、それを進歩させることはできません。

ありがとうございます!

EDIT:

私の春のセキュリティ設定:

@Configuration 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private MongoDBAuthenticationProvider authenticationProvider; 

    @Autowired 
    public void globalUserDetails(final AuthenticationManagerBuilder auth) throws Exception { 
    auth.authenticationProvider(authenticationProvider); 
    } 

    @Override 
    @Bean 
    public AuthenticationManager authenticationManagerBean() throws Exception { 
    return super.authenticationManagerBean(); 
    } 
} 

私のOAuth設定:Serverで

@Configuration 
@EnableAuthorizationServer 
public class OAuth2AuthorizationServerConfig extends  AuthorizationServerConfigurerAdapter { 

    @Autowired 
    @Qualifier("authenticationManagerBean") 
    private AuthenticationManager authenticationManager; 

    @Override 
    public void configure(final AuthorizationServerSecurityConfigurer oauthServer) throws Exception { 
     oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()"); 
    } 

    @Override 
    public void configure(final ClientDetailsServiceConfigurer clients) throws Exception { 
    clients 
    .inMemory().withClient("web") 
    .redirectUris("http://localhost:3000") 
    .secret("secret") 
    .authorizedGrantTypes("implicit", "refresh_token").scopes("read", "write") 
    .accessTokenValiditySeconds(3600).refreshTokenValiditySeconds(2592000); 
    } 

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

例外:

2016-05-25 19:52:20.744 DEBUG 34968 --- [nio-8080-exec-5] .s.o.p.e.FrameworkEndpointHandlerMapping : Looking up handler method for path /oauth/authorize 
2016-05-25 19:52:20.744 DEBUG 34968 --- [nio-8080-exec-5] .s.o.p.e.FrameworkEndpointHandlerMapping : Returning handler method [public org.springframework.web.servlet.ModelAndView org.springframework.security.oauth2.provider.endpoint.AuthorizationEndpoint.authorize(java.util.Map<java.lang.String, java.lang.Object>,java.util.Map<java.lang.String, java.lang.String>,org.springframework.web.bind.support.SessionStatus,java.security.Principal)] 
2016-05-25 19:52:20.746 DEBUG 34968 --- [nio-8080-exec-5] o.s.s.w.a.ExceptionTranslationFilter  : Authentication exception occurred; redirecting to authentication entry point 

org.springframework.security.authentication.InsufficientAuthenticationException: User must be authenticated with Spring Security before authorization can be completed. 
at  org.springframework.security.oauth2.provider.endpoint.AuthorizationEndpoint.authorize(AuthorizationEndpoint.java:138) ~[spring-security-oauth2-2.0.9.RELEASE.jar:na] 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_40] 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_40] 
.... 

答えて

1

あなたは認証サーバを呼び出すとために 暗黙のグラント・タイプでは、csrf攻撃を回避するために、状態パラメータとして不透明な文字列値を含める必要があります。

http://localhost:8080/oauth/authorize?grant_type=implicit&client_id=web&response_type=token&redirect_uri=http%3A%2F%2Flocalhost%3A3000&state=123abc

あなたは状態パラメータに言及した値が応答であなたに戻ってエコーバックされますので、認証サーバへのリクエストURLは次のようになります。エコーされた値と初期値を比較して、csrf攻撃が発生していないことを確認します。

ありがとう、 Soma。

+0

ご回答ありがとうございます@Soma。私は "国家"のパラムについて聞いたことがあるが、それは私の場合に関係することをまだ処理していなかった。私はそれを修正しましたが、私はまだ誤りがあります。 私はそれをPOSTとして発行しました。私はcsrfエラーを受け取りました。私はアクセス拒否を取得しました。質問をいくつかのコードで更新し、エラーが見つかるかどうか確認します。どうもありがとう! – user1294431

+0

要求はGETでなければなりません。ちょうど確認するために、あなたはクライアントを登録しましたか? GETで詳細なエラーメッセージを投稿してください。ありがとうございます –

+0

私は答えを編集しました。あなたのご協力と時間をいただきありがとうございます。 – user1294431

関連する問題