Мой внутренний сервер является весенним приложением RESTFull, и в классе контроллеров, к которому я хочу получить доступ, CrossOrigin разрешен с
@CrossOrigin
Проблема: я не могу получить доступ к API из моего Frontend с VueJS.Запросы выполняются с помощью библиотеки Axios.Моя ошибка:
Доступ к XMLHttpRequest по адресу http://localhost:8000/places/notverified' из источника 'http://localhost:8080' заблокирован политикой CORS: Ответ на предпечатный запрос не проходит проверку контроля доступа: У него нет статуса HTTP ok.
Мой запрос в Javascript:
axios('http://localhost:8000/' + path, {
method: 'GET',
mode: 'no-cors',
headers: {
'Accept': 'application/json',
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + accessToken
}
}).then(response => {
console.log(response)
}).catch(error => {
console.log(error)
})
Я уже все перепробовал, как вы, вероятно, можете в поле заголовка Access Control Allow Origin, потому чтоэто только для ответа, а не для запроса.
Позже сработало следующее:
В моем весеннем приложении я отредактировал свой WebSecurityConfigurerAdapter и добавил следующее:
http.authorizeRequests().antMatchers("/places/notverified").permitAll()
С этим методом он впоследствии работал.Но может ли это быть решением?Является ли authorizeRequests «приемлемым» для использования?
SecurityConfigurerAdapter
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private JwtTokenProvider jwtTokenProvider;
@Override
protected void configure(HttpSecurity http) throws Exception {
// Disable CSRF (cross site request forgery)
http.csrf().disable();
// No session will be created or used by spring security
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
// Entry points
http.authorizeRequests()//
.antMatchers("/users/signin").permitAll()//
.antMatchers("/users/adminsignin").permitAll()
.antMatchers("/users/signup").permitAll()//
.anyRequest().authenticated();
// If a user try to access a resource without having enough permissions
http.exceptionHandling().accessDeniedPage("/login");
http.apply(new JwtTokenFilterConfigurer(jwtTokenProvider));
// http.httpBasic();
}
@Override
public void configure(WebSecurity web) {
web.ignoring().antMatchers("/v2/api-docs")//
.antMatchers("/swagger-resources/**")//
.antMatchers("/swagger-ui.html")//
.antMatchers("/configuration/**")//
.antMatchers("/webjars/**")//
.antMatchers("/public")
.antMatchers("/mail/registrationConfirm");
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(12);
}
@Bean(name = BeanIds.AUTHENTICATION_MANAGER)
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
Надеюсь, вы поможете мне.