Swagger UI Invalid CORS request; Origin=null from Tomcat

1,613 views
Skip to first unread message

tim...@gmail.com

unread,
Jul 22, 2014, 3:27:55 PM7/22/14
to swagger-sw...@googlegroups.com
Hi,

I have downloaded SwaggerUI and am attempting a initial test by running from:
C:\Users\me\Downloads\swagger-ui-master\dist

I can get to the pets api-docs just fine, however when I point to the same URL above I get:

Can't read from server. It may not have the appropriate access-control-origin settings.


I've updated a simple Rest Tutorial to jersey2/maven and  Swagger annotations.
I am using Firefox and verifying via RESTClient plugin.
I can send GET and receive a valid response, so the REST api is working.
I can send a GET request to the http://localhost:8081/TodoRest/rest/api-docs swagger api-docs  via RESTClient and I get swagger/json back
{"apiVersion":"1.0.0","swaggerVersion":"1.2","apis":[{"path":"/todo","description":"Operations about Todos"}]}


I captured the communication with fiddler, note the Origin: null  the null is an ascii string "null"
GET http://localhost:8081/TodoRest/rest/api-docs HTTP/1.1
Host: localhost:8081
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0
Accept: application/json
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT
: 1
Origin: null
Connection: keep-alive

HTTP
/1.1 403 Forbidden
Server: Apache-Coyote/1.1
Content-Type: text/plain
Content-Length: 0
Date: Tue, 22 Jul 2014 18:09:15 GMT



I did a bunch of googling and found that I should be able to turn on a tomcat filter in the
\tomcat\apache-tomcat-7.0.54\conf\web.xml, but it doesn't seem to work.

<filter>
 
<filter-name>CorsFilter</filter-name>
 
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>

   
<!-- init-param>
    <param-name>cors.allowed.origins</param-name>
    <!-- tried several different param-value combinations -->
    <param-value>"null"</param-value>  <!-- tried null with quotes -->

    <param-value>*</param-value>  <!-- tried * -->
    <param-value>*, null</param-value>  <!-- tried *, null -->
    <param-value>null</param-value>  <!-- tried  null --> 
  </init-param -->

</filter>
<filter-mapping>
 
<filter-name>CorsFilter</filter-name>
 
<url-pattern>/*</url-pattern>
</filter-mapping>


I bumbed the logging.properties
org.apache.catalina.filters.CorsFilter.level = FINEST

this shows  the following at the Tomcat console
FINE: Invalid CORS request; Origin=null;Method=GET

So, I assume I have configured something incorrectly, but I can't figure out what it is.
If you can tell me how to handle Origin=null either in tomcat or via swagger that would be great!

Thanks,
Tim

Ron

unread,
Jul 22, 2014, 3:52:03 PM7/22/14
to swagger-sw...@googlegroups.com
Hi Tim,

I can't relate to the Tomcat CORS filter as I've never used it, but the Swagger samples show you how to do it yourself.
This is the code for the CORS filter you need to include - https://github.com/wordnik/swagger-core/blob/master/samples/java-jaxrs/src/main/java/com/wordnik/swagger/sample/util/ApiOriginFilter.java
And the definition in the web.xml would be - https://github.com/wordnik/swagger-core/blob/master/samples/java-jaxrs/src/main/webapp/WEB-INF/web.xml#L50 and https://github.com/wordnik/swagger-core/blob/master/samples/java-jaxrs/src/main/webapp/WEB-INF/web.xml#L59




--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

tim...@gmail.com

unread,
Jul 22, 2014, 6:23:24 PM7/22/14
to swagger-sw...@googlegroups.com
Hi Ron,
Thanks for your reply. 
I have the UI up with some of the json description,
My responses are now blank,  I probably have some typos to fix.

I will update this when I get it working.

Thanks,
Tim
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

tim...@gmail.com

unread,
Jul 23, 2014, 1:37:19 AM7/23/14
to swagger-sw...@googlegroups.com
Ron,

Thanks again for your help, the swagger page is up and running.
I had the base path and the jersey url-pattern messed up. with those fixed I can now access/run 
my swagger.

Tim

tim...@gmail.com

unread,
Jul 23, 2014, 1:41:06 AM7/23/14
to swagger-sw...@googlegroups.com
I did find the following : https://gist.github.com/knutwalker/7913344#file-web-xml
see web.xml line 31. this is using the CorsFilter, but I haven't had the time to check it out.
Reply all
Reply to author
Forward
0 new messages