2017-04-03 152 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」:http://localhost:8080?code=XXX
  5. 然後我複製代碼,並使用谷歌高級REST客戶端發送令牌請求:對http://localhost:9000/oauth/token?client_id=test&grant_type=authorization_code&code=XXX POST沒有任何標題。據我瞭解海報應該使用瀏覽器cookie。
  6. 由於令牌請求的結果,我看到一個彈出式窗口要求填寫用戶名和密碼,同時期望獲得訪問令牌作爲響應。

Poster popup

請幫我解決這個問題。我應該爲我的令牌請求添加一些標頭嗎?或者我的授權服務器配置不正確?

回答

0

我自己發現了這個問題的原因,只是閱讀OAuth2規範的其他資源。 它需要發送授權令牌請求與以下值:

Authorization: Basic {base64 encode of clientId:clientSecret} 
相關問題