Browse Source

更新修改

chenweida 7 years ago
parent
commit
6a8819f8fb

+ 1 - 1
patient-co/patient-co-wlyy/src/main/java/com/yihu/wlyy/config/MvcConfig.java

@ -15,7 +15,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
 * Created by chenweida on 2017/4/6.
 */
@Configuration
@Profile({"test", "prod"})
//@Profile({"test", "prod"})
public class MvcConfig extends WebMvcConfigurerAdapter {
    private Logger logger = LoggerFactory.getLogger(MvcConfig.class);

+ 49 - 0
patient-co/patient-co-wlyy/src/main/java/com/yihu/wlyy/interceptors/GateWayAOP.java

@ -0,0 +1,49 @@
package com.yihu.wlyy.interceptors;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.json.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
/**
 * 由于用了@ResponseBody之后 拦截器返回值ModelAndView 是null 所以用线程变量解决
 */
@Aspect
@Component
public class GateWayAOP {
    private  ThreadLocal entityThreadLocal=new ThreadLocal();
    //Controller层切点路径
    @Pointcut("execution(* com.yihu.wlyy.web..*.*(..))")
    public void controllerAspect() {
    }
    public GateWayAOP() {
        //System.out.println("Observer---------------------------------------");
    }
    @Around("controllerAspect()")
    public Object checkToken(ProceedingJoinPoint point) throws Throwable {
        Object obj = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        try {
            obj = point.proceed();
        }catch (Exception e){
            e.printStackTrace();
        }
        request.setAttribute("returnObj",obj);
        return obj;
    }
}

+ 7 - 1
patient-co/patient-co-wlyy/src/main/java/com/yihu/wlyy/interceptors/GateWayInterceptor.java

@ -49,6 +49,7 @@ public class GateWayInterceptor implements HandlerInterceptor {
    @Autowired
    private GcTokenDao gcTokenDaoDao;
    /**
     * preHandle:预处理回调方法
     *
@ -114,8 +115,13 @@ public class GateWayInterceptor implements HandlerInterceptor {
        String output = "";
        if (modelAndView != null) {
            output = JSONObject.fromObject(modelAndView.getModelMap()).toString();
        } else {
            Object returnObj = request.getAttribute("returnObj");
            if (returnObj != null) {
                output = JSONObject.fromObject(returnObj).toString();
            }
        }
        HandlerMethod handlerMethod= (HandlerMethod) handler;
        HandlerMethod handlerMethod = (HandlerMethod) handler;
//        response.getOutputStream()
        String token = request.getHeader("accesstoken");
        String ip = NetworkUtil.getIpAddress(request);