先後端分離開發中一個小問題(集成403後跨域問題)

解決跨域的三種方式nginx

 

- Jsonp:基於script中的src屬性實現ajax

- nginx:反向代理(部署):利用nginx反向代理把跨域爲不跨域,支持各類請求方式spring

- CORS:瀏覽器會將ajax請求分爲兩類,其處理方案略有差別:簡單請求、特殊請求。(相似於中間商,將請求處理過了,再分配)跨域

這裏我是用的CORS,由於springmvc中已經集成了:瀏覽器

@Configuration
public class GlobalCorsConfig {
    @Bean
    public CorsFilter corsFilter() {
        //1.添加CORS配置信息
        CorsConfiguration config = new CorsConfiguration();
        //1) 容許的域,不要寫*,不然cookie就沒法使用了
        config.addAllowedOrigin("http://127.0.0.1:8080");
        //2) 是否發送Cookie信息
        config.setAllowCredentials(true);
        //3) 容許的請求方式
        config.addAllowedMethod("OPTIONS");
        config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("DELETE");
        config.addAllowedMethod("PATCH");
        // 4)容許的頭信息
        config.addAllowedHeader("*");
        //2.添加映射路徑,咱們攔截一切請求
        UrlBasedCorsConfigurationSource configSource = new
                UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**", config);
        //3.返回新的CorsFilter.
        return new CorsFilter(configSource);
    }
}

但即便都準備好了,測試的時候仍是403cookie

緣由是請求路徑不能再是localhost了,要修改成127.0.0.1mvc

相關文章
相關標籤/搜索