Così ho la seguente autorizzazione del server condensato dal this example da Dave SyerStandalone Primavera OAuth2 JWT Autorizzazione Server + CORS
@SpringBootApplication
public class AuthserverApplication {
public static void main(String[] args) {
SpringApplication.run(AuthserverApplication.class, args);
}
/* added later
@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
protected static class MyWebSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http //.csrf().disable()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/oauth/token").permitAll();
}
}*/
@Configuration
@EnableAuthorizationServer
protected static class OAuth2AuthorizationConfig extends
AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
KeyPair keyPair = new KeyStoreKeyFactory(
new ClassPathResource("keystore.jks"), "foobar".toCharArray())
.getKeyPair("test");
converter.setKeyPair(keyPair);
return converter;
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("acme")
//.secret("acmesecret")
.authorizedGrantTypes(//"authorization_code", "refresh_token",
"password").scopes("openid");
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints)
throws Exception {
endpoints.authenticationManager(authenticationManager).accessTokenConverter(
jwtAccessTokenConverter());
}
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer)
throws Exception {
oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess(
"isAuthenticated()");
}
}
}
quando l'eseguo e testarlo con l'arricciatura
curl [email protected]:8110/oauth/token -d grant_type=password -d client_id=acme -d username=user -d password=password
ho un JWT come risposta, ma non appena provo ad accedere a AuthServer dal mio frontend (Angular JS su una porta diversa) ottengo l'errore CORS. Non a causa di intestazioni mancanti, ma perché la richiesta OPTION viene rifiutata e mancano le credenziali.
Request URL:http://localhost:8110/oauth/token
Request Method:OPTIONS
Status Code:401 Unauthorized
WWW-Authenticate:Bearer realm="oauth", error="unauthorized", error_description="Full authentication is required to access this resource"
sapevo già che devo aggiungere un CorsFilter e inoltre trovato this post dove ho usato il frammento per la prima risposta di lasciare l'accesso richiesta OPZIONI /oauth/token
senza credenziali:
@Order(-1)
public class MyWebSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/oauth/token").permitAll();
}
}
Dopo di che ho avuto con l'arricciatura il seguente errore:
{"timestamp":1433370068120,"status":403,"error":"Forbidden","message":"Expected CSRF token not found. Has your session expired?","path":"/oauth/token"}
Quindi, per rendere le cose semplici ho solo aggiunto http.csrf().disable()
al 0.123.metodo della classe MyWebSecurity, che risolve il problema con la richiesta OPTION, ma quindi la richiesta POST non funziona più e ottengo There is no client authentication. Try adding an appropriate authentication filter.
(anche con arricciatura).
Ho cercato di scoprire se devo in qualche modo collegare la classe MyWebSecurity e l'AuthServer, ma senza fortuna. L'esempio originale (link all'inizio) inietta anche AuthenticManager, ma questo non ha cambiato nulla per me.
In alternativa, è possibile utilizzare il 'CorsFilter' fornito con Spring Framework 4.2+ e Spring Boot 1.3+, vedere il mio [risposta qui] (http: // StackOverflow.it/a/31748398/1092077) per ulteriori dettagli. –
@ SébastienDeleuze Il CorsFilter di Spring sembra essere eseguito dopo FilterChain per qualche motivo e risulta ancora non autorizzato :( – froginvasion
Hai provato a cambiare l'ordine dei filtri come descritto [qui] (http://stackoverflow.com/a/26147788/1092077)? –