2016-04-11 36 views
4

spring-boot çerçevesini kullanarak bir web hizmetini başarıyla oluşturdum. Benim araştırmaya göreYay Güvenliği OAuth2 InvalidGrantException

, bahar bir erişim belirteci (baseURL/oauth/token) istemek için varsayılan-url çeşit sağlamaktadır: Şimdi OAuth2'ye (kullanarak yay) ile benim web hizmetini güvenli ve ilgili bir kaç soru var istiyorum. Postacı kullanarak URL'yi test ettim ve geçerli bir erişim belirteci (client_credentials hibe türü kullanarak) iade edildi, ancak yenileme belirteci yok. Ancak bu yöntem aşağıdaki hata yanıtta grant_type=password ve sonuçlarla çalışmaz:

{"error":"invalid_grant","error_description":"Bad credentials"}

My bahar uygulama günlükleri InvalidGrantException.

Ben grant_type=password sınamak için kullanılan bukle şudur: o grant_type=password desteklemediği için

curl -v -X POST -H "Content-Type: application/json" -H "Authorization: Basic base64encodedclientidandsecret" 'http://localhost:8888/oauth/token?grant_type=password&username=user&password=1234' 

Ben postacı kullanarak test etmedi.

Hem accessToken hem de refreshToken'u grant_type=password kullanarak dönmek için nasıl kaynak alabilirim?

Yapılandırmamda bir sorun var mı? aşağıdaki gibi (yapılandırma)

My bahar uygulama görünüyor:

@Configuration 
@ComponentScan 
@EnableAutoConfiguration(exclude = { MongoAutoConfiguration.class, MongoDataAutoConfiguration.class }) 
@SpringBootApplication 
public class CsWebServerApplication { 

    public static final String RESOURCE_ID = "myresource"; 

    public static final String CLIENT_ID = "myapplication"; 
    public static final String CLIENT_SECRET = "application_secret"; 

    public static void main(String[] args) { 

     SpringApplication.run(MyWebServerApplication.class, args); 
    } 

    @Configuration 
    @EnableAuthorizationServer 
    protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter { 

     @Inject 
     private AuthenticationManager authenticationManager; 

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

     @Override 
     public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 

      clients.inMemory().withClient(CLIENT_ID) 
      .authorizedGrantTypes("client_credentials", "password", "refresh_token") 
      .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT") 
      .scopes("read", "write", "trust") 
      .secret(CLIENT_SECRET); 
     } 

     @Override 
     public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception { 
      super.configure(oauthServer); 
     } 
    } 

    @Configuration 
    @EnableResourceServer 
    protected static class ResourceConfig extends ResourceServerConfigurerAdapter { 

     @Override 
     public void configure(HttpSecurity http) throws Exception { 

      http.requestMatchers().antMatchers("/*", "/admin/beans").and().authorizeRequests().anyRequest() 
       .access("#oauth2.hasScope('read')"); 
     } 

     @Override 
     public void configure(ResourceServerSecurityConfigurer resources) throws Exception { 
      resources.resourceId(RESOURCE_ID); 
     } 
    } 

    @Configuration 
    @EnableWebSecurity 
    protected static class WebConfigurer extends WebSecurityConfigurerAdapter { 

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      super.configure(http); 
     } 

     @Override 
     public void configure(WebSecurity webSecurity) throws Exception { 
      webSecurity.ignoring() 
        // All of Spring Security will ignore the requests 
        .antMatchers("/accessibleservices/**") 
     } 
    } 

} 

cevap

1

"4.3.2. Access Token Request" RFC 6749 içinde (OAuth 2.0 Yetkilendirme Çerçevesi) aşağıdaki gibi diyor.

istemci "uygulama/x-www-form-urlencoded" UTF-8 bir karakter kodlamasına sahip ek B başına biçimini kullanarak aşağıdaki parametreleri ekleyerek belirteç uç için bir istek yapan HTTP istek varlığı vücut:

Yani -H "Content-Type: application/json" yanlıştır. Ayrıca, curl komut satırınız yanlış. POST için bir form parametresi belirtmek üzere -d seçeneğini kullanın.

0

Yenileme belirteçlerinin desteklenmesi hakkında. Varsayılan olarak DefaultTokenServices sınıfını kullanarak bahar oauth ve varsayılan olarak devre dışı bırakılmış yenileme jetonlarının desteği. Başlatma işlemini OAuth2Config.class ürününüzde geçersiz kılmalısınız.

Örnek:

@Bean 
    @Primary 
    public DefaultTokenServices tokenServices() { 
     DefaultTokenServices tokenServices = new DefaultTokenServices(); 
     tokenServices.setSupportRefreshToken(true); 
     return tokenServices; 
    } 
İlgili konular