JCors is a simple and flexible Java implementation of W3C CORS spec.
See more details about CORS here
CORS stands for Cross-Origin Resource Sharing and it’s a new way to enable client-side cross-origin requests.
Before CORS it was (almost) impossible to make Ajax requests to another domain because of ‘Same Origin Policy’ enforced by Javascript.
Some newest browsers already implement CORS support in order to make cross-domain client-side requests possible without the need of using hacks (such JSON-P).
JCors is a small lib that provides a simple Servlet Filter that enables CORS request handling by existing Java resources.
These resources can be anything like webservices (REST / SOAP), Servlets, Struts Actions…
Just add this to your web.xml file:
<filter> <filter-name>CorsFilter</filter-name> <filter-class>org.jcors.web.CorsEnablingFilter</filter-class> </filter>
<filter-mapping> <filter-name>CorsFilter</filter-name> <url-pattern>/resource/*</url-pattern> </filter-mapping>
And optionally, add a file jcors.xml to your classpath
Check the jcors-sample.xml here
Now you can get JCors from Maven Central Repository:
<dependency>
<groupId>org.jcors</groupId>
<artifactId>jcors</artifactId>
<version>1.2</version>
</dependency>
There are some things to improve on this project, can you help me?
- Improve unit tests
- Servlet 3.0 API support
- Request Headers validation
Email me: diegossilveira at gmail.com
Follow me: @diegossilveira