2017-04-03 9 views
1

OAuth 2.0の認証コード許可フローを実装しようとしています。しかし、トークン要求の認証ポップアップの問題に立ち往生してください。スプリングセキュリティトークン要求に認証が必要です

ここに私のコードです。

@SpringBootApplication 
public class Main { 
    public static void main(String[] args) { 
     SpringApplication.run(Main.class, args); 
    } 
} 

@Configuration パブリッククラスSecurityConfig WebSecurityConfigurerAdapter {

@Override 
public void init(AuthenticationManagerBuilder auth) throws Exception { 
    auth.inMemoryAuthentication() 
     .withUser("admin").password("abc").roles("ADMIN"); 
} 

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http.authorizeRequests() 
      .antMatchers("/login").permitAll() 
      .anyRequest().authenticated() 
      .and() 
      .formLogin().permitAll() 
      .and().csrf().disable(); 
} 

}

@Configuration 
@EnableAuthorizationServer 
public class AuthServerOAuth2Config 
     extends AuthorizationServerConfigurerAdapter { 

    @Autowired 
    private AuthenticationManager authenticationManager; 

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

    @Override 
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
     clients.inMemory() 
       .withClient("test") 
       .secret("test_secret") 
       .authorizedGrantTypes("authorization_code") 
       .scopes("write"); 
    } 

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

    @Bean 
    public TokenStore tokenStore() { 
     return new InMemoryTokenStore(); 
    } 

    @Bean 
    protected AuthorizationCodeServices authorizationCodeServices() { 
     return new InMemoryAuthorizationCodeServices(); 
    } 
} 

私は、次の手順を実行しますトークン取得するにはを拡張:

  1. ブラウザに行くの使用: http://localhost:9000/oauth/authorize?response_type=code&client_id=test&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F&scope=write

  2. まず、それは私がユーザ名とPASSORDを入力してログインフォームに私をリダイレクトします。私は私の「テストへのアクセス権を提供することを可能にする場合は、管理者ABC

  3. そして、それが求められます"クライアント。
  4. それは「URIをリダイレクトする」ために私をリダイレクトします:
  5. http://localhost:8080?code=XXXは、その後、私は、コードをコピーして、トークン要求を送信するためにGoogleの高度な休息クライアントを使用:http://localhost:9000/oauth/token?client_id=test&grant_type=authorization_code&code=XXX に POSTを任意のヘッダーなし。私が理解する限り、PosterはブラウザのCookieを使用する必要があります。
  6. トークン要求の結果、私は、応答としてアクセストークンを取得することを期待している間に、ユーザー名とパスワードを入力するポップアップが表示されます。

Poster popup

、問題を解決するために私を助けてください。トークンリクエストにいくつかのヘッダーを追加する必要がありますか?または、認可サーバーの設定が間違っていますか?

答えて

0

OAuth2仕様の他のリソースを読んでいるだけで、問題の原因がわかりました。 トークン要求の認証要求を次の値で送信する必要があります。

Authorization: Basic {base64 encode of clientId:clientSecret} 
関連する問題