I am using the following code to set CORS for Java/Jersey based web application.
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletResponse resp = (HttpServletResponse) response;
// resp.addHeader("Access-Control-Allow-Origin", "http://localhost:3000");
resp.addHeader("Access-Control-Allow-Origin", "*");
// resp.addHeader("Access-Control-Allow-Origin", "http://digitran-virtualtestengineer.tk");
resp.addHeader("Access-Control-Allow-Headers", "*");
resp.addHeader("Access-Control-Allow-Methods", "*");
chain.doFilter(request, response);
}
My client code is ReactJS based and the API call is made as follows:
axios.post("http://localhost:9900/upload/file", data, config )
Problem: Every time I have to switch between localhost and domain by using the following lines of code:
resp.addHeader("Access-Control-Allow-Origin", "http://localhost:3000");
resp.addHeader("Access-Control-Allow-Origin", "http://digitran-virtualtestengineer.tk");
or I have to allow all domains as follows:
resp.addHeader("Access-Control-Allow-Origin", "*");
Question:
Is there any way to add both localhost and domain in the same line like the following code?
resp.addHeader("Access-Control-Allow-Origin", ["http://digitran-virtualtestengineer.tk", "http://localhost:3000"]);