psh
1 year ago
8 changed files with 99 additions and 87 deletions
@ -1,56 +1,56 @@ |
|||||
package org.nl.config.saconfig; |
//package org.nl.config.saconfig;
|
||||
|
//
|
||||
import org.springframework.core.annotation.Order; |
//import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component; |
//import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.cors.CorsConfiguration; |
//import org.springframework.web.cors.CorsConfiguration;
|
||||
|
//
|
||||
import javax.servlet.*; |
//import javax.servlet.*;
|
||||
import javax.servlet.http.HttpServletRequest; |
//import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse; |
//import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException; |
//import java.io.IOException;
|
||||
|
//
|
||||
/** |
///**
|
||||
* 跨域过滤器 |
// * 跨域过滤器
|
||||
* @author kong |
// * @author kong
|
||||
*/ |
// */
|
||||
@Component |
//@Component
|
||||
@Order(-200) |
//@Order(-200)
|
||||
public class CorsFilter implements Filter { |
//public class CorsFilter implements Filter {
|
||||
|
//
|
||||
static final String OPTIONS = "OPTIONS"; |
// static final String OPTIONS = "OPTIONS";
|
||||
|
//
|
||||
@Override |
// @Override
|
||||
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) |
// public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
|
||||
throws IOException, ServletException { |
// throws IOException, ServletException {
|
||||
HttpServletRequest request = (HttpServletRequest) req; |
// HttpServletRequest request = (HttpServletRequest) req;
|
||||
HttpServletResponse response = (HttpServletResponse) res; |
// HttpServletResponse response = (HttpServletResponse) res;
|
||||
// 允许指定域访问跨域资源
|
// // 允许指定域访问跨域资源
|
||||
response.setHeader("Access-Control-Allow-Origin", "*"); |
// response.setHeader("Access-Control-Allow-Origin", "*");
|
||||
// 允许所有请求方式
|
//// // 允许所有请求方式
|
||||
response.setHeader("Access-Control-Allow-Methods", "*"); |
//// response.setHeader("Access-Control-Allow-Methods", "*");
|
||||
// 有效时间
|
//// // 有效时间
|
||||
response.setHeader("Access-Control-Max-Age", "3600"); |
//// response.setHeader("Access-Control-Max-Age", "3600");
|
||||
// 允许的header参数
|
//// // 允许的header参数
|
||||
response.setHeader("Access-Control-Allow-Headers", "*"); |
//// response.setHeader("Access-Control-Allow-Headers", "*");
|
||||
response.setHeader("Access-Control-Allow-Credentials", "true"); |
// response.setHeader("Access-Control-Allow-Credentials", "true");
|
||||
|
//
|
||||
// 如果是预检请求,直接返回
|
// // 如果是预检请求,直接返回
|
||||
if (OPTIONS.equals(request.getMethod())) { |
// if (OPTIONS.equals(request.getMethod())) {
|
||||
System.out.println("=======================浏览器发来了OPTIONS预检请求=========="); |
// System.out.println("=======================浏览器发来了OPTIONS预检请求==========");
|
||||
response.getWriter().print(""); |
// response.getWriter().print("");
|
||||
return; |
// return;
|
||||
} |
// }
|
||||
|
//
|
||||
// System.out.println("*********************************过滤器被使用**************************");
|
// // System.out.println("*********************************过滤器被使用**************************");
|
||||
chain.doFilter(req, res); |
// chain.doFilter(req, res);
|
||||
} |
// }
|
||||
|
//
|
||||
@Override |
// @Override
|
||||
public void init(FilterConfig filterConfig) { |
// public void init(FilterConfig filterConfig) {
|
||||
} |
// }
|
||||
|
//
|
||||
@Override |
// @Override
|
||||
public void destroy() { |
// public void destroy() {
|
||||
} |
// }
|
||||
|
//
|
||||
} |
//}
|
||||
|
Loading…
Reference in new issue