|
@@ -1,68 +1,107 @@
|
|
|
-package cn.efunbox.audio.aop;
|
|
|
-
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-import org.springframework.util.StringUtils;
|
|
|
-import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
-import org.springframework.web.servlet.ModelAndView;
|
|
|
-
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
-
|
|
|
-/**
|
|
|
- * Created by libin on 2015/5/13.
|
|
|
- */
|
|
|
-@Component
|
|
|
-public class AllowOriginIntercepter implements HandlerInterceptor {
|
|
|
- private static final Logger logger = LoggerFactory.getLogger(AllowOriginIntercepter.class);
|
|
|
- private String allowValue = "precms.api.ai160.com,precms.ai160.com,cms.api.ai160.com,cms.ai160.com,efunbox.cn,cms.api.efunbox.cn,*";
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|
|
-
|
|
|
- String origin = request.getHeader("Origin");
|
|
|
- //logger.info("进入AllowOriginIntercepter 拦截 origin={}",origin);
|
|
|
- String headers = request.getHeader("Access-Control-Request-Headers");
|
|
|
- if (!StringUtils.isEmpty(headers)) {
|
|
|
- headers = ", " + headers;
|
|
|
- } else {
|
|
|
- headers = "";
|
|
|
- }
|
|
|
- if (!StringUtils.isEmpty(origin)) {
|
|
|
- if (originIsAllow(origin, allowValue)) {
|
|
|
- response.setHeader("Access-Control-Allow-Origin", origin);
|
|
|
- response.setHeader("Access-Control-Allow-Methods", "GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE, PATCH");
|
|
|
- response.setHeader("Access-Control-Allow-Headers", "Origin, X-Token, Authentication, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization, Cache-control, id, token "+headers);
|
|
|
- response.setHeader("Access-Control-Allow-Credentials", "true");
|
|
|
- }
|
|
|
- }
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- private boolean originIsAllow(String origin, String value) {
|
|
|
- if (StringUtils.isEmpty(value)) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- if (value.contains("*")) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- String[] values = value.split(",");
|
|
|
- for (String s : values) {
|
|
|
- if (origin.indexOf(s) > -1) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-}
|
|
|
+package cn.efunbox.audio.aop;
|
|
|
+
|
|
|
+import cn.efunbox.audio.utils.ApiResult;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.http.HttpMethod;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
+import org.springframework.web.servlet.ModelAndView;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.PrintWriter;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by libin on 2015/5/13.
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class AllowOriginIntercepter implements HandlerInterceptor {
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(AllowOriginIntercepter.class);
|
|
|
+ private String allowValue = "precms.api.ai160.com,precms.ai160.com,cms.api.ai160.com,cms.ai160.com,efunbox.cn,cms.api.efunbox.cn,*";
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|
|
+
|
|
|
+ String origin = request.getHeader("Origin");
|
|
|
+ //logger.info("进入AllowOriginIntercepter 拦截 origin={}",origin);
|
|
|
+ String headers = request.getHeader("Access-Control-Request-Headers");
|
|
|
+
|
|
|
+ //忽略 OPTIONS 请求
|
|
|
+ if (HttpMethod.OPTIONS.matches(request.getMethod())) {
|
|
|
+ logger.info("OPTIONS 请求 忽略 返回200");
|
|
|
+ //将实体对象转换为JSON Object转换
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
+ response.setStatus(HttpStatus.OK.value());
|
|
|
+ if (null!=origin){
|
|
|
+ response.setHeader("Access-Control-Allow-Origin", origin);
|
|
|
+ }else {
|
|
|
+ response.setHeader("Access-Co..." +
|
|
|
+ "ntrol-Allow-Origin", "*");
|
|
|
+ }
|
|
|
+ response.setContentType("application/json; charset=utf-8");
|
|
|
+ response.setHeader("Access-Control-Allow-Credentials", "true");
|
|
|
+ response.setHeader("Access-Control-Allow-Methods", "GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE, PATCH");
|
|
|
+ response.setHeader("Access-Control-Max-Age", "3600");
|
|
|
+ response.setHeader("Access-Control-Allow-Headers", "Origin, X-Token,Authentication, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization, Cache-control, "+headers);
|
|
|
+ response.setHeader("X-Frame-Options","SAMEORIGIN");
|
|
|
+ PrintWriter out = null;
|
|
|
+ try {
|
|
|
+ out = response.getWriter();
|
|
|
+ out.append(JSON.toJSONString(ApiResult.ok()));
|
|
|
+ } catch (IOException e) {
|
|
|
+ logger.error("request ={} 返回 失败 e={}" ,request.getRequestURI(),e);
|
|
|
+ } finally {
|
|
|
+ if (out != null) {
|
|
|
+ out.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!StringUtils.isEmpty(headers)) {
|
|
|
+ headers = ", " + headers;
|
|
|
+ } else {
|
|
|
+ headers = "";
|
|
|
+ }
|
|
|
+ if (!StringUtils.isEmpty(origin)) {
|
|
|
+ if (originIsAllow(origin, allowValue)) {
|
|
|
+ response.setHeader("Access-Control-Allow-Origin", origin);
|
|
|
+ response.setHeader("Access-Control-Allow-Methods", "GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE, PATCH");
|
|
|
+ response.setHeader("Access-Control-Allow-Headers", "Origin, X-Token, Authentication, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization, Cache-control, id, token "+headers);
|
|
|
+ response.setHeader("Access-Control-Allow-Credentials", "true");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean originIsAllow(String origin, String value) {
|
|
|
+ if (StringUtils.isEmpty(value)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (value.contains("*")) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ String[] values = value.split(",");
|
|
|
+ for (String s : values) {
|
|
|
+ if (origin.indexOf(s) > -1) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|