Я строю сервис API с помощью Spring Boot. Он использует базовую аутентификацию для аутентификации. Когда клиенты пытаются подключиться к API, они получат ошибку Cors < /strong>. < /P>
На пружине он вызывает ошибку < /p>
Java.lang.illegalargumentExexpect «Заголовок" Access-Control-Allow-Origin ". Чтобы разрешить
учетные данные для набора источников, перечислите их явно или рассмотрите
, используя «AllorIginPatterns» вместо этого. Даже для его документа -https: //docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/corsregistration.html#allowedoriginpatterns-java.lang. config.AllowEdorigInpatterns ();
Ниже приведен мой код corsfilter,
@Configuration
public class RequestCorsFilter {
@Bean
public CorsFilter corsFilter() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.setAllowedOrigins(Collections.singletonList("*"));
config.setAllowedHeaders(Arrays.asList("Origin", "Content-Type", "Accept", "responseType", "Authorization"));
config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH"));
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}
< /code>
И вот мой код аутентификации, < /p>
@Configuration
@EnableWebSecurity
public class AuthenConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth)
throws Exception {
auth
.inMemoryAuthentication()
.withUser("thor").password("{noop}P@ssw00rd")
.authorities("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
String[] AUTH_WHITELIST = {
// -- swagger ui
"/v2/api-docs",
"/swagger-resources/**",
"/configuration/ui",
"/configuration/security",
"/swagger-ui.html",
"/webjars/**"
};
http
.csrf().disable()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.antMatchers(AUTH_WHITELIST).permitAll() // whitelist URL permitted
.antMatchers("/api").authenticated(); // others need auth
}
}
Подробнее здесь: https://stackoverflow.com/questions/660 ... g-security