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