Chinaunix首页 | 论坛 | 博客
  • 博客访问: 335136
  • 博文数量: 297
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3065
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(297)

文章存档

2022年(95)

2021年(201)

2019年(1)

我的朋友

分类: Java

2022-01-19 11:48:03


点击(此处)折叠或打开


  1. ackage org.fh.config;

  2. import java.io.IOException;

  3. import javax.servlet.Filter;
  4. import javax.servlet.FilterChain;
  5. import javax.servlet.FilterConfig;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.ServletRequest;
  8. import javax.servlet.ServletResponse;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;

  11. import org.springframework.stereotype.Component;

  12. /**
  13.  * 说明:跨域访问处理
  14.  * 作者:FH Admin
  15.  * from:fhadmin.cn
  16.  */
  17. @Component
  18. public class CORSFilter implements Filter {

  19.     @Override
  20.     public void init(FilterConfig filterConfig) throws ServletException {

  21.     }

  22.     @Override
  23.     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
  24.             throws IOException, ServletException {

  25.         HttpServletRequest request = (HttpServletRequest) servletRequest;
  26.         HttpServletResponse response = (HttpServletResponse) servletResponse;
  27.         response.setContentType("textml;charset=UTF-8");
  28.         response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
  29.         response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
  30.         response.setHeader("Access-Control-Max-Age", "0");
  31.         response.setHeader("Access-Control-Allow-Headers","Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With,userId,token");
  32.         response.setHeader("Access-Control-Allow-Credentials", "true"); // 是否支持cookie跨域        
  33.         response.setHeader("XDomainRequestAllowed", "1");
  34.         filterChain.doFilter(servletRequest, servletResponse);

  35.     }

  36.     @Override
  37.     public void destroy() {

  38.     }
  39. }


阅读(277) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~