2016-04-11 20 views
4

Ich habe erfolgreich einen Web-Service mit dem spring-boot Framework erstellt. Jetzt möchte ich meinen Web-Service mit OAuth2 (mit Feder) sichern und habe ein paar Fragen dazu:Spring Security OAuth2 InvalidGrantException

Nach meinen Recherchen bietet Frühjahr eine Art von Standard-URL, um ein Zugriffs-Token (baseURL/oauth/token) anzufordern. Ich habe die URL mit dem Postboten getestet und ein gültiges Zugriffstoken zurückgegeben (mit dem Client-Berechtigungsnachweis client_credentials), aber kein Aktualisierungstoken. Allerdings ist diese Methode nicht mit grant_type=password und die Ergebnisse in der folgenden Fehlerreaktion arbeiten:

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

Meine Feder Anwendungsprotokolle InvalidGrantException.

Die Locke habe ich grant_type=password zu testen, ist die folgende:

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' 

ich nicht Postbote mit nicht getestet, weil es nicht grant_type=password unterstützt leider.

Wie bekomme ich Frühling, um sowohl accessToken als auch refreshToken mit grant_type=password zurückzugeben?

Ist irgendetwas mit meiner Konfiguration nicht in Ordnung?

Meine Feder Anwendung (Konfiguration) sieht wie folgt aus:

@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/**") 
     } 
    } 

} 

Antwort

1

"4.3.2. Access Token Request" in RFC 6749 (Die OAuth 2.0 Authorization Framework) sagt wie folgt.

Der Client macht eine Anforderung an den Token-Endpunkt, indem die folgenden Parameter unter Verwendung des "application/x-www-form-urlencoded" Format pro Appendix B mit einer Zeichencodierung von UTF-8 in die HTTP Anfrage Entity-Body:

So ist -H "Content-Type: application/json" falsch. Darüber hinaus ist die Curl-Befehlszeile falsch. Verwenden Sie die Option -d, um einen Formularparameter für POST anzugeben.

0

Über die Unterstützung von Aktualisierungstoken. Standardmäßig wird oauth mit der DefaultTokenServices-Klasse und der Unterstützung von Aktualisierungstokens standardmäßig deaktiviert. Sie sollten die Initialisierung in Ihrer OAuth2Config.class überschreiben.

Beispiel:

@Bean 
    @Primary 
    public DefaultTokenServices tokenServices() { 
     DefaultTokenServices tokenServices = new DefaultTokenServices(); 
     tokenServices.setSupportRefreshToken(true); 
     return tokenServices; 
    } 
Verwandte Themen