Поэтому я не могу настроить cors. Мое приложение angular не может отправить запрос api из-за ошибки. Я могу выполнять запросы с помощью soapUI, и они работают нормально. Но из браузера есть:
Доступ к XMLHttpRequest в 'http://localhost:8080/backend/api/public' из origin 'http://localhost:4200' заблокирован политикой CORS: No 'Access-Control-Allow-Origin 'присутствует в запрошенном ресурсе.
Я искал ответы, и они похожи на мой класс.
Я использую весеннюю загрузку веб-безопасности.
@EnableWebSecurity
@Configuration
public class AuthConfig extends WebSecurityConfigurerAdapter {
@Value(value = "${auth0.apiAudience}")
private String apiAudience;
@Value(value = "${auth0.issuer}")
private String issuer;
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("http://localhost:4200"));
configuration.setAllowedMethods(Arrays.asList("GET","POST"));
configuration.setAllowCredentials(true);
configuration.addAllowedHeader("Authorization");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**",configuration);
return source;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
JwtWebSecurityConfigurer
.forRS256(apiAudience, issuer)
.configure(http)
.authorizeRequests()
.antMatchers(HttpMethod.GET,"/api/public").permitAll()
.antMatchers(HttpMethod.GET, "/api/private").authenticated()
.antMatchers(HttpMethod.GET, "/api/private-scoped").hasAnyAuthority("read:posts");
}
}
Этот bean-компонент должен добавить этот заголовок cors, но этого не происходит. Может быть, вы знаете, как это сделать получше? В чем разница между WebSecurityConfigurerAdapter и WebMvcConfigurerAdapter?