Автономный Spring OAuth2 Сервер авторизации JWT + 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()");
}
}
}
когда я запускаю его и проверяю с помощью curl
curl acme@localhost:8110/oauth/token -d grant_type=password -d client_id=acme -d username=user -d password=password
Я получаю JWT в качестве ответа, но как только я пытаюсь получить доступ к AuthServer с моего внешнего интерфейса (Angular JS на другом порту), я получаю ошибку CORS. Не потому, что отсутствуют заголовки, а потому, что запрос OPTION отклонен и отсутствует учетные данные.
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"
Я уже знал, что я должен добавить CorsFilter и дополнительно нашелэта почта где я использовал фрагмент для первого ответа, чтобы позволить опциям запросить доступ/oauth/token
без учетных данных:
@Order(-1)
public class MyWebSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/oauth/token").permitAll();
}
}
После этого я получил с curl следующую ошибку:
{"timestamp":1433370068120,"status":403,"error":"Forbidden","message":"Expected CSRF token not found. Has your session expired?","path":"/oauth/token"}
Так что для простоты я просто добавилhttp.csrf().disable()
кconfigure
метод класса MyWebSecurity, который решает проблему с запросом OPTION, но поэтому запрос POST больше не работает, и я получаюThere is no client authentication. Try adding an appropriate authentication filter.
(также с завитком).
Я попытался выяснить, нужно ли мне каким-то образом соединить класс MyWebSecurity и AuthServer, но безуспешно. Исходный пример (ссылка в начале) также вводит аутентификационный менеджер, но для меня это ничего не изменило.