<del id="d4fwx"><form id="d4fwx"></form></del>
      <del id="d4fwx"><form id="d4fwx"></form></del><del id="d4fwx"><form id="d4fwx"></form></del>

            <code id="d4fwx"><abbr id="d4fwx"></abbr></code>
          • Springboot跨域設置實例詳解

            定義:跨域是指從一個域名的網(wǎng)頁去請求另一個域名的資源

            成都創(chuàng)新互聯(lián)長期為1000+客戶提供的網(wǎng)站建設服務,團隊從業(yè)經(jīng)驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為壽縣企業(yè)提供專業(yè)的網(wǎng)站建設、成都網(wǎng)站建設壽縣網(wǎng)站改版等技術服務。擁有10多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

            1.原由

            公司內(nèi)部有多個不同的子域,比如一個是location.company.com ,而應用是放在app.company.com , 這時想從 app.company.com去訪問 location.company.com 的資源就屬于跨域

            本人是springboot菜鳥,但是做測試框架后端需要使用Springboot和前端對接,出現(xiàn)跨域問題,需要設置后端Response的Header.走了不少坑,在這總結一下以備以后使用

            2.使用場景

            瀏覽器默認不允許跨域訪問,包括我們平時ajax也是限制跨域訪問的。

            產(chǎn)生跨域訪問的情況主要是因為請求的發(fā)起者與請求的接受者1、域名不同;2、端口號不同

            如果一個網(wǎng)頁可以隨意地訪問另外一個網(wǎng)站的資源,那么就有可能在客戶完全不知情的情況下出現(xiàn)安全問題

            3.解決方案

            通過設置Access-Control-Allow-Origin來實現(xiàn)跨域訪問

            4.具體解決

            剛開始使用http://www.jianshu.com/p/f2060a6d6e3b設置,但是由于我們使用的spring版本的問題,CorsConfiguration類需要4.2.7版本。和我們使用的spring里面版本不一致,導致版本沖突或者各種問題

            @Configuration
            public class CorsConfig {
              private CorsConfiguration buildConfig() {
                CorsConfiguration corsConfiguration = new CorsConfiguration();
                corsConfiguration.addAllowedOrigin("*"); // 1
                corsConfiguration.addAllowedHeader("*"); // 2
                corsConfiguration.addAllowedMethod("*"); // 3
                return corsConfiguration;
              }
            
              @Bean
              public CorsFilter corsFilter() {
                UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
                source.registerCorsConfiguration("/**", buildConfig()); // 4
                return new CorsFilter(source);
              }
            }

            后來改為Filter方式

            @Component
            public class CorsFilter implements Filter {
              final static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(CorsFilter.class);
            
              public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
                HttpServletResponse response = (HttpServletResponse) res;
                HttpServletRequest reqs = (HttpServletRequest) req;
                response.setHeader("Access-Control-Allow-Origin","*");
                response.setHeader("Access-Control-Allow-Credentials", "true");
                response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
                response.setHeader("Access-Control-Max-Age", "3600");
                response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
                chain.doFilter(req, res);
              }
              public void init(FilterConfig filterConfig) {}
              public void destroy() {}
            }

            后來改為Filter方式

            @Component
            public class CorsFilter implements Filter {
              final static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(CorsFilter.class);
              public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
                HttpServletResponse response = (HttpServletResponse) res;
                HttpServletRequest reqs = (HttpServletRequest) req;
                response.setHeader("Access-Control-Allow-Origin","*");
                response.setHeader("Access-Control-Allow-Credentials", "true");
                response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
                response.setHeader("Access-Control-Max-Age", "3600");
                response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
                chain.doFilter(req, res);
              }
              public void init(FilterConfig filterConfig) {}
              public void destroy() {}
            }

            網(wǎng)上很多資料都是教按以上方法設置,但是我這里就是設置不成功的。出現(xiàn)下面問題

            <span >Fetch API cannot load https://atfcapi.alpha.elenet.me/atfcapi/project/mainPageList. The value of the 'Access-Control-Allow-Origin' </span> 
            <span >header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'https://atfcapi-test.faas.elenet.me' is therefore not allowed access.</span> 

            目前為止,不知道為什么這樣配置不可以,然后改為設置單個域名,如下顯示

            response.setHeader("Access-Control-Allow-Origin", "https://atfcapi-test.faas.elenet.me"); 

            這樣設置就成功了,但是我們有好幾個環(huán)境,同一套代碼,寫死一個域名并解決不了問題,
            按照很多網(wǎng)絡文章中所說,設置多個域名如下:

            response.setHeader("Access-Control-Allow-Origin", "https://atfcapi-test.faas.elenet.me,https://atfcapi-test-beta.faas.elenet.me"); 

            但是設置完以后,并不好用,出現(xiàn)如下錯誤信息:

            <span >Fetch API cannot load https://atfcapi.alpha.elenet.me/atfcapi/project/getProjects. The 'Access-Control-Allow-Origin' header contains multiple values </span> 
            <span >'https://atfcapi-test.faas.elenet.me,https://atfcapi-test-beta.faas.elenet.me', but only one is allowed. Origin 'https://atfcapi-test.faas.elenet.me' is therefore not allowed access. Have the server send the header with a valid value, or, if an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.</span> 

            設置為以下方式也還是錯誤:

            response.setHeader("Access-Control-Allow-Origin", "https://atfcapi-test.faas.elenet.me"); 
                response.setHeader("Access-Control-Allow-Origin", "https://atfcapi-test-beta.faas.elenet.me"); 

            最后采用了一種和設置為* 的方式一樣的辦法,代碼如下:

            @Component
            public class CorsFilter implements Filter {
              final static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(CorsFilter.class);
              public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
                HttpServletResponse response = (HttpServletResponse) res;
                HttpServletRequest reqs = (HttpServletRequest) req;
                response.setHeader("Access-Control-Allow-Origin",reqs.getHeader("Origin"));
                response.setHeader("Access-Control-Allow-Credentials", "true");
                response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
                response.setHeader("Access-Control-Max-Age", "3600");
                response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
                chain.doFilter(req, res);
              }
              public void init(FilterConfig filterConfig) {}
              public void destroy() {}
            }

            從request 中的header中獲取Origin,來做配置,最終成功并滿足需求。
            其實有些東西還是一知半解,但是起碼曲線救國也是一種解決方法。

            總結

            以上就是本文關于Spring boot跨域設置實例詳解的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:

            快速了解Spring Boot

            SpringBoot集成多數(shù)據(jù)源解析

            Maven管理SpringBoot Profile詳解

            如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

            當前標題:Springboot跨域設置實例詳解
            網(wǎng)頁URL:http://www.jbt999.com/article40/psppho.html

            成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷手機網(wǎng)站建設定制網(wǎng)站自適應網(wǎng)站外貿(mào)建站移動網(wǎng)站建設

            廣告

            聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

            成都seo排名網(wǎng)站優(yōu)化

              <del id="d4fwx"><form id="d4fwx"></form></del>
              <del id="d4fwx"><form id="d4fwx"></form></del><del id="d4fwx"><form id="d4fwx"></form></del>

                    <code id="d4fwx"><abbr id="d4fwx"></abbr></code>
                  • 蜜桃视频网站免费观看 | 国产又粗又猛 | 亚洲人一级电影 | 小黄片高清无码 | 夜夜操夜夜爱 |