I am using jersey 2.18 for developing rest api. (using tomcat container)
I want to allow access to clients from other domain.
So I am trying below code to allow cross domain requests.
Filter
public class MyCorsFilter implements Filter {
public MyCorsFilter() { }
public void init(FilterConfig fConfig) throws ServletException { }
public void destroy() { }
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
((HttpServletResponse)response).addHeader("Access-Control-Allow-Origin", "*");
chain.doFilter(request, response);
}
}
web.xml
<filter>
<filter-name>MyCorsFilter</filter-name>
<filter-class>MyCorsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyCorsFilter</filter-name>
<url-pattern>/api/*</url-pattern>
</filter-mapping>
Above code works fine until I add HTTP basic authentication.
When I add basic authentication I am getting following error:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
When checked headers using developer tools I found following:
Please note that the error is while executing OPTIONS
method. (I am using GET
method)
Any suggestion on how to add allow CORS with basic HTTP authentication will be appreciated.