java 前后端分离跨域问题

java跨域实现类

  1. package com.sq.filter;  
  2.   
  3. import org.springframework.stereotype.Component;  
  4.   
  5. import javax.servlet.*;  
  6. import javax.servlet.http.HttpServlet;  
  7. import javax.servlet.http.HttpServletResponse;  
  8. import java.io.IOException;  
  9.   
  10.   
  11. @Component  
  12. public class AccessFilter extends HttpServlet implements Filter {  
  13.   
  14.     private static final long serialVersionUID = 1L;  
  15.   
  16.   
  17.     @Override  
  18.     public void init(FilterConfig filterConfig) throws ServletException {  
  19.   
  20.     }  
  21.   
  22.     @Override  
  23.     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {  
  24.   
  25.         HttpServletResponse httpResponse = (HttpServletResponse) response;  
  26.         httpResponse.setHeader("Access-Control-Allow-Origin""*");  
  27.         httpResponse.setHeader("Access-Control-Allow-Methods""GET, POST, PUT, DELETE");  
  28.         httpResponse.setHeader("Access-Control-Max-Age""1800");  
  29.         httpResponse.setHeader("Access-Control-Allow-Headers""Origin, X-Requested-With, Content-Type, Accept");  
  30.         httpResponse.setHeader("Access-Control-Allow-Credentials""true");  
  31.         chain.doFilter(request, httpResponse);  
  32.     }  
  33. }  

   web.xml 配置:

  1.      <filter>  
  2.          <filter-name>crosfilter-name>  
  3.          <filter-class>com.sq.filter.AccessFilterfilter-class>  
  4.      filter>  
  5.      <filter-mapping>  
  6.          <filter-name>crosfilter-name>  
  7.          <url-pattern>/*url-pattern>  
  8.      filter-mapping>  



  1. java跨域实现类:

你可能感兴趣的:(java前端)