2015-09-09 103 views
1

有人可以告訴我爲什麼我會收到這些錯誤。Swagger UI上的CORS問題

GET http://127.0.0.1:9000/api-docs/service.json 

    200 OK 4ms swagger-ui.js (line 30261) 
    Unable to Load SwaggerUI /api-docs/ (line 83) 
    Cross-Origin Request Blocked: The Same Origin Policy disallows 
    reading the remote resource at http://127.0.0.1:9000/api- 
    docs/service.json. This can be fixed by moving the resource to the 
    same domain or enabling CORS. 
    uncaught exception: Can't read from server. It may not have the 
appropriate access-control-origin settings. 

我試圖在端口運行揚鞭UI說9090和9000揚鞭API文檔,並試圖在UI中顯示的文檔。

我已經在API文檔服務器(端口9000)上添加了CORS過濾器,如下所示。

FilterHolder cors = swaggerUIContext.addFilter(CrossOriginFilter.class,"/*",EnumSet.of(DispatcherTyp‌ e.REQUEST)); 
cors.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "*");  
cors.setInitParameter(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, ""); 
cors.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "GET,POST,HEAD"); 
cors.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM, "Content-Type, api_key, Authorization"); 

在Firefox V33.0的請求和響應頭是

Response Headers 
    Content-Length 428 
    Content-Type application/json 

    Request Headers 
    Accept application/json;charset=utf-8,*/* 
    Accept-Encoding gzip, deflate 
    Accept-Language en-US,en;q=0.5 
    Connection keep-alive 
    Host localhost:9000 
    Origin http://localhost:9090 
    Referer http://localhost:9090/api-docs/ 
    User-Agent Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:33.0)  
    Gecko/20100101 Firefox/33.0 

這裏是我如何在服務器上設置CORS

 final ResourceHandler swaggerUIResourceHandler = new ResourceHandler(); 
    swaggerUIResourceHandler.setResourceBase("target/classes/api-docs"); 
    final ServletContextHandler swaggerUIContext = new ServletContextHandler(); 
    swaggerUIContext.setContextPath("/api-docs"); 
    swaggerUIContext.setHandler(swaggerUIResourceHandler); 

    FilterHolder cors = swaggerUIContext.addFilter(CrossOriginFilter.class,"/*",EnumSet.of(DispatcherType.REQUEST)); 
    cors.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "*"); 
    cors.setInitParameter(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*"); 
    cors.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "GET,POST,HEAD"); 
    cors.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM, "Content-Type, api_key, Authorization"); 

    ServletHolder def = new ServletHolder("default", DefaultServlet.class); 
    def.setInitParameter("resourceBase","./http/"); 
    def.setInitParameter("dirAllowed","false"); 
    swaggerUIContext.addServlet(def,"/"); 

    HandlerList handlers = new HandlerList(); 

handlers.setHandlers(new Handler[] { swaggerUIContext, new DefaultHandler() }); 
server.setHandler(handlers); 
+0

你可能想在http://enable-cors.org/或http://www.html5rocks.com/en/瞭解更多關於CORS tutorials/cors/ – Marged

+0

我確定你的Firefox有版本? ;-)請使用Firefox的F工具創建網絡跟蹤並向我們顯示標題 – Marged

+0

因此,您的設置不會導致設置正確的標題。你能看到另一個實例上的這些頭文件嗎?也許你把他們放在了錯誤的地方。 – Marged

回答

1

你用json文件做了些什麼時髦嗎?

我遇到了同樣的錯誤,試圖修改我的JSON文件並查看Chrome上的更改。確保json本身不會破壞,額外的括號,逗號等等。我從swagger現場演示中的示例json開始從頭開始,我很好。我知道這是一項任務,但爲我工作,至少加載了UI!

您也可以通過去招搖UI自述,CORS support section

+0

我原來是一個腐敗的JSON文件,我正在使用前夕招搖並不是使用默認/ api文檔JSON文件我用了一個測試其中有額外的字符。謝謝(你的)信息! – ShahNewazKhan