I’d like to add the following HTTP header to all responses stemming from a request for a font-file in Spring MVC:
Access-Control-Allow-Origin: *
So, I know how to set up a simple static request mapping…
<mvc:resources mapping="/fonts/**" location="/fonts/" />
But how can I add the necessary header? I know that I could implement a controller that responds to all /fonts/ requests and adds the header, but that seems like major overkill. Is there something simpler/more lightweight?
There is no need to implement a special controller for this. You can use an interceptor which extends
HandlerInterceptorAdapter.The postHandle method is passed the
HttpServletResponse. You can set the header there. This interceptor can be configured to apply to requests to a specific path.See the Spring docs here.