Так что я не могу настроить Cors. Мое угловое приложение не может отправить запрос API из-за ошибки.
Я могу делать запросы по soapUI, и они работают нормально. Но из браузера есть:
Доступ к XMLHttpRequest в «http://localhost:8080/backend/api/public' от источника» http://localhost:4200' заблокирован политикой CORS: в запрошенном ресурсе отсутствует заголовок «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");
}
}
Этот бин должен добавить этот заголовок cors, но это не так. Может быть, вы знаете какую-нибудь лучшую идею сделать это?
В чем разница между WebSecurityConfigurerAdapter и WebMvcConfigurerAdapter?