Skip to content

Instantly share code, notes, and snippets.

@zeroows
Last active October 4, 2022 11:44
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • Save zeroows/80bbe076d15cb8a4f0ad to your computer and use it in GitHub Desktop.
Save zeroows/80bbe076d15cb8a4f0ad to your computer and use it in GitHub Desktop.
To enable CORS support in Spring MVC 4 - Access-Control-Allow-Origin
package com.alkhodiry.mb.rest.filters;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.filter.OncePerRequestFilter;
/**
* Enabling CORS support - Access-Control-Allow-Origin
* @author zeroows@gmail.com
*
* <code>
<!-- Add this to your web.xml to enable "CORS" -->
<filter>
<filter-name>cors</filter-name>
<filter-class>com.elm.mb.rest.filters.CORSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>cors</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
* </code>
*/
public class CORSFilter extends OncePerRequestFilter {
private static final Log LOG = LogFactory.getLog(CORSFilter.class);
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
response.addHeader("Access-Control-Allow-Origin", "*");
if (request.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equals(request.getMethod())) {
LOG.trace("Sending Header....");
// CORS "pre-flight" request
response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
// response.addHeader("Access-Control-Allow-Headers", "Authorization");
response.addHeader("Access-Control-Allow-Headers", "Content-Type");
response.addHeader("Access-Control-Max-Age", "1");
}
filterChain.doFilter(request, response);
}
}
@vaughnjavier
Copy link

Thanks, this really helped out allot!

@akaluzinski
Copy link

Thank you. It is simple and very useful.

@immime
Copy link

immime commented May 28, 2015

Thank you very much, this worked for me!

@an1z
Copy link

an1z commented Jun 5, 2015

Thanks

@alessioarsuffi
Copy link

Hi, thanks for the snippet, do you know if it is possible to activate / deactivate the filter for example by changing a property ? i mean via java class not via xml.

Thanks

@kivicko
Copy link

kivicko commented Jul 9, 2015

i am looking to every piece of code to fix my problem. This one seems nice. THANK YOU! You saved my day.

@amolskh
Copy link

amolskh commented Mar 26, 2016

I am using similar code.. My request is getting through but my Authorization Header is coming as null
I have uncommented the line

@khubilla
Copy link

khubilla commented Jun 6, 2016

Thanks

@margrop
Copy link

margrop commented Oct 18, 2016

Thanks

@Byakuyana
Copy link

Thanks!

@lalchandmali
Copy link

Not working for me. I am unable to allow Authorization Header

@Ramdharan
Copy link

Great! Thank you so much.

@fabianGV
Copy link

Gracias funciono muy bien

@ft2130854
Copy link

thank you so much . its work for me

@beanlee
Copy link

beanlee commented Apr 28, 2017

Thanks, It helped me :)
XML Config also can do the same thing at Spring 4.2.x
Spring Doc

@gmiskos
Copy link

gmiskos commented Jun 12, 2017

Where shall we put that class?

@ottort-23
Copy link

Funiona ! me ha salvado el dia gracias.

@manuja
Copy link

manuja commented Dec 8, 2017

Thank you so much. You saved my day !!!!!!

@arjunkori
Copy link

Awesome...

@kleeb
Copy link

kleeb commented Mar 2, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment