Quindi ho il seguente server di authorization condensato da questo esempio di Dave Syer
@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 lo eseguo e lo collaudo con l’arricciatura
curl [email protected]:8110/oauth/token -d grant_type=password -d client_id=acme -d username=user -d password=password
Ricevo 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 ho trovato anche questo post in cui ho usato lo snippet per la prima risposta per consentire alle OPTIONS di accedere /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 che ho ottenuto con 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 semplificare ho appena aggiunto http.csrf().disable()
al metodo configure
della class 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.
There is no client authentication. Try adding an appropriate authentication filter.
(anche con arricciatura).
Ho provato a scoprire se devo in qualche modo colbind la class MyWebSecurity e l’AuthServer, ma senza fortuna. L’esempio originale (link all’inizio) inietta anche AuthenticManager, ma questo non ha cambiato nulla per me.