否';访问控制允许原点';请求的资源(Spring)上存在标头
我知道论坛上有很多关于这个问题的帖子,但仍然没有找到解决方案 因此,我在vps上的私有JVM/tomcat 8.5.30中部署了两个应用程序。一个是我的ROOT.war,另一个是admin.war,它们可以从和访问 在我安装ssl证书之前,一切正常。在安装它并强制https重定向之后,我的admin.war遇到了一个问题(现在它们都可以从和访问) 我的管理员处理很多jquery(我无法更改),每次我试图提交一些东西时都会遇到这个错误否';访问控制允许原点';请求的资源(Spring)上存在标头,spring,spring-security,cors,Spring,Spring Security,Cors,我知道论坛上有很多关于这个问题的帖子,但仍然没有找到解决方案 因此,我在vps上的私有JVM/tomcat 8.5.30中部署了两个应用程序。一个是我的ROOT.war,另一个是admin.war,它们可以从和访问 在我安装ssl证书之前,一切正常。在安装它并强制https重定向之后,我的admin.war遇到了一个问题(现在它们都可以从和访问) 我的管理员处理很多jquery(我无法更改),每次我试图提交一些东西时都会遇到这个错误 Access to XMLHttpRequest at 'ht
Access to XMLHttpRequest at 'http: //example.com/admin/add' from origin 'https: //example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
所以我试图通过spring security解决这个问题。在我的安全配置中,我有
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
public class SiteSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors().and()
//.....
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("https://example.com"));
configuration.setAllowedHeaders(Arrays.asList("Access-Control-Allow-Headers"));
configuration.addExposedHeader("Access-Control-Allow-Headers");
configuration.setAllowedMethods(Arrays.asList("POST"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
}
我对我的root应用程序和admin应用程序都这样做(我不知道这对他们两个是否都正确)。还是不行
有什么帮助吗
谢谢 如果您看到错误
'http://example.com/admin/add' from origin 'https://example.com' has been blocked
有两个问题
1我猜您的/add
API调用没有被重定向到https。理想情况下,它应该是https://example.com/admin/add
要么解决此问题
或
2将管理员应用程序中的setAllowedOriginates
更改为http,如下所示
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("https://example.com", "http://example.com"));
configuration.setAllowedHeaders(Arrays.asList("Access-Control-Allow-Headers"));
configuration.addExposedHeader("Access-Control-Allow-Headers");
configuration.setAllowedMethods(Arrays.asList("POST"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}