【发布时间】:2019-05-17 17:08:46
【问题描述】:
我总是在前端遇到这种情况,说从源“localhost://2800/api/abc/xyz”访问 XMLHttpRequest 在“api/abc/xyz”已被 CORS 策略阻止:对预检请求的响应没有通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。
我尝试将此添加到后端 `
http.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS).permitAll()
.anyRequest().authenticated();
`
还有我的前端 AJAX CALL API: `
$.ajax({
'type': 'GET',
'url': '/api/vessel/?vesselId=' + $('#vessel_id').val() + '&page=1&size=100',
'headers': {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": `Bearer ${session}`
},
success: function(response) {console.log(response)}
});
`
但是在那之后我仍然遇到那个错误。
`
@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(HttpMethod.OPTIONS).permitAll()
// Disallow everything else..
.anyRequest().authenticated();
// If a user try to access a resource without having enough permissions
//http.exceptionHandling().accessDeniedPage("/login");
// Apply JWT
http.apply(new JwtTokenFilterConfigurer(jwtTokenProvider));
// Optional, if you want to test the API from a browser
// http.httpBasic();
}
`
【问题讨论】: