Created
July 25, 2013 05:51
-
-
Save kjunine/6077196 to your computer and use it in GitHub Desktop.
CrossOriginResourceSharingInterceptor When using @responsebody, response header SHOULD be set in the preHandler method.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import javax.servlet.http.HttpServletRequest; | |
| import javax.servlet.http.HttpServletResponse; | |
| import org.springframework.web.servlet.HandlerInterceptor; | |
| import org.springframework.web.servlet.ModelAndView; | |
| public class CrossOriginResourceSharingInterceptor implements | |
| HandlerInterceptor { | |
| private static final String DEFAULT_ALLOW_ORIGIN = "*"; | |
| private static final String DEFAULT_ALLOW_METHODS = "*"; | |
| private static final int DEFAULT_MAX_AGE = 86400; | |
| private String allowOrigin = DEFAULT_ALLOW_ORIGIN; | |
| private String allowMethods = DEFAULT_ALLOW_METHODS; | |
| private int maxAge = DEFAULT_MAX_AGE; | |
| public CrossOriginResourceSharingInterceptor setAllowOrigin( | |
| String allowOrigin) { | |
| this.allowOrigin = allowOrigin; | |
| return this; | |
| } | |
| public CrossOriginResourceSharingInterceptor setAllowMethods( | |
| String allowMethods) { | |
| this.allowMethods = allowMethods; | |
| return this; | |
| } | |
| public CrossOriginResourceSharingInterceptor setMaxAge(int maxAge) { | |
| this.maxAge = maxAge; | |
| return this; | |
| } | |
| @Override | |
| public boolean preHandle(HttpServletRequest request, | |
| HttpServletResponse response, Object handler) throws Exception { | |
| response.setHeader("Access-Control-Allow-Origin", allowOrigin); | |
| response.setHeader("Access-Control-Allow-Methods", allowMethods); | |
| response.setHeader("Access-Control-Max-Age", String.valueOf(maxAge)); | |
| return true; | |
| } | |
| @Override | |
| public void postHandle(HttpServletRequest request, | |
| HttpServletResponse response, Object handler, | |
| ModelAndView modelAndView) throws Exception { | |
| } | |
| @Override | |
| public void afterCompletion(HttpServletRequest request, | |
| HttpServletResponse response, Object handler, Exception ex) | |
| throws Exception { | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment