2017-04-03 154 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} 
相关问题