package com.ronglian.bms.controller.mobile;
import org.apache.http.HttpStatus;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ResponceFilter implements Filter {
public void init(FilterConfig var1){
System.out.println("========================================");
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain){
try {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
//跨域请求,*代表允许全部类型
response.setHeader("Access-Control-Allow-Origin", "*");
// //允许请求方式
// response.setHeader(“Access-Control-Allow-Methods”, “POST, GET, OPTIONS, DELETE”);
// //用来指定本次预检请求的有效期,单位为秒,在此期间不用发出另一条预检请求
// response.setHeader(“Access-Control-Max-Age”, “3600”);
// //请求包含的字段内容,如有多个可用哪个逗号分隔如下
// response.setHeader(“Access-Control-Allow-Headers”,
// “content-type,x-requested-with,Authorization,” +
// " x-ui-request,lang");
// //访问控制允许凭据,true为允许
// response.setHeader(“Access-Control-Allow-Credentials”,
// “true”);
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, " +
"WG-App-Version, WG-Device-Id, WG-Network-Type, WG-Vendor," +
" WG-OS-Type, WG-OS-Version, WG-Device-Model, WG-CPU, WG-Sid, " +
"WG-App-Id, WG-Token");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Allow-Credentials", "true");
// 浏览器是会先发一次options请求,如果请求通过,则继续发送正式的post请求
// 配置options的请求返回
if (request.getMethod().equals("OPTIONS")) {
response.setStatus(HttpStatus.SC_OK);
response.getWriter().write("OPTIONS returns OK");
return;
}
filterChain.doFilter(servletRequest, servletResponse);
} catch (IOException e) {
e.printStackTrace();
} catch (ServletException e) {
e.printStackTrace();
}
System.out.println("========================================");
}
public void destroy(){
System.out.println("========================================");
}
}