Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java cors issue

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * Servlet Filter implementation class CORSFilter
 */
// Enable it for Servlet 3.x implementations
/* @ WebFilter(asyncSupported = true, urlPatterns = { "/*" }) */
public class CORSFilter implements Filter {
 
  /**
   * Default constructor.
   */
  public CORSFilter() {
    // TODO Auto-generated constructor stub
  }
 
  /**
   * @see Filter#destroy()
   */
  public void destroy() {
    // TODO Auto-generated method stub
  }
 
  /**
   * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
   */
  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
      throws IOException, ServletException {
 
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    System.out.println("CORSFilter HTTP Request: " + request.getMethod());
 
    // Authorize (allow) all domains to consume the content
    ((HttpServletResponse) servletResponse).addHeader("Access-Control-Allow-Origin", "*");
    ((HttpServletResponse) servletResponse).addHeader("Access-Control-Allow-Methods","GET, OPTIONS, HEAD, PUT, POST");
 
    HttpServletResponse resp = (HttpServletResponse) servletResponse;
 
    // For HTTP OPTIONS verb/method reply with ACCEPTED status code -- per CORS handshake
    if (request.getMethod().equals("OPTIONS")) {
      resp.setStatus(HttpServletResponse.SC_ACCEPTED);
      return;
    }
 
    // pass the request along the filter chain
    chain.doFilter(request, servletResponse);
  }
 
  /**
   * @see Filter#init(FilterConfig)
   */
  public void init(FilterConfig fConfig) throws ServletException {
    // TODO Auto-generated method stub
  }
 
}
Comment

PREVIOUS NEXT
Code Example
Java :: Algorithms - Max value 
Java :: java.lang.noclassdeffounderror: failed resolution of: lorg/apache/http/protocolversion; 
Java :: try catch not working java 
Java :: staging and production environment logging spring boot 
Java :: java jpanel popup message 
Java :: add element to arraylist of arraylist in java 
Java :: java find nth smallest element using priority queue heap 
Java :: problemi arrayList javafx 
Java :: java default keyword 
Java :: java private access modifier 
Java :: java declare and populate array 
Java :: list all android sensors in java 
Java :: stop countdown timer when exiting an activity 
Java :: place.getlatlng() returning null 
Java :: change password swing gui 
Java :: multipleQuastion.Java 
Java :: RecyclerView scrolled UP/DOWN listener 
Java :: set attribute java 
Java :: java unshift 
Java :: java windowbuilder launch on second monitor 
Java :: ezra margolin 
Java :: ordenar numeros java 
Java :: spring security not going to logout success urk 
Java :: spigot change move speed of living entity creature 
Java :: ternärer operator java 
Java :: how to load template file from resource folder in spring boot project 
Java :: change FS to hdfs java 
Java :: java commands in different packages not working 
Java :: Recyclerview scramble after scrolling 
Java :: exemple javafx 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =