Browse Source

Merge branch 'dev' of http://192.168.1.220:10080/Amoy2/wlyy2.0 into dev

Trick 6 years ago
parent
commit
22d4137737

+ 0 - 54
svr/svr-patient/src/main/java/com/yihu/jw/patient/interceptor/BaseInterceptor.java

@ -1,54 +0,0 @@
package com.yihu.jw.patient.interceptor;
import com.yihu.jw.security.dao.TokenDao;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
abstract public class BaseInterceptor implements HandlerInterceptor {
	private Logger logger= LoggerFactory.getLogger(BaseInterceptor.class);
	@Autowired
	public TokenDao tokenDao;
	public JSONObject getAgent(HttpServletRequest request) {
		try {
			String userAgent = request.getHeader("userAgent");
			if (StringUtils.isEmpty(userAgent)) {
				userAgent = request.getHeader("User-Agent");
			}
			logger.info("userAgent:" + userAgent);
			return new JSONObject(userAgent);
		} catch (Exception e) {
			return null;
		}
	}
	public String error(int code, String msg) {
		try {
			JSONObject json = new JSONObject();
			json.put("status", code);
			json.put("msg", msg);
			return json.toString();
		} catch (Exception e) {
			return null;
		}
	}
	public String write(int code, String msg) {
		try {
			JSONObject json = new JSONObject();
			json.put("status", code);
			json.put("msg", msg);
			return json.toString();
		} catch (Exception e) {
			return null;
		}
	}
}

+ 0 - 68
svr/svr-patient/src/main/java/com/yihu/jw/patient/interceptor/GateWayAOP.java

@ -1,68 +0,0 @@
package com.yihu.jw.patient.interceptor;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
/**
 * 由于用了@ResponseBody之后 拦截器返回值ModelAndView 是null 所以用线程变量解决
 */
@Aspect
@Component
public class GateWayAOP {
    private Logger logger = LoggerFactory.getLogger(GateWayAOP.class);
    //Controller层切点路径
    @Pointcut("execution(* com.yihu.jw.patient.endpoint..*.*(..))")
    public void controllerAspect() {
    }
    public GateWayAOP() {
    }
    @Around("controllerAspect()")
    public Object checkToken(ProceedingJoinPoint point) throws Throwable {
        Object obj = null;
        HttpServletRequest request = null;
        try {
            request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            obj = point.proceed();
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        if (request != null) {
            request.setAttribute("returnObj", obj);
        }
        return obj;
    }
}
enum Flag {
    close("0"),
    open("1");
    Flag(String value) {
        this.value = value;
    }
    private String value;
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
}

+ 0 - 153
svr/svr-patient/src/main/java/com/yihu/jw/patient/interceptor/PatientInterceptor.java

@ -1,153 +0,0 @@
package com.yihu.jw.patient.interceptor;
import com.yihu.jw.entity.base.security.Token;
import com.yihu.jw.patient.constant.SystemConstant;
import com.yihu.jw.patient.log.InterfaceCallLogs;
import com.yihu.jw.util.date.DateUtil;
import com.yihu.jw.utils.SystemData;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.Date;
/**
 * 患者权限校验
 *
 * @author George
 */
@Component
public class PatientInterceptor extends BaseInterceptor {
    private Logger logger = LoggerFactory.getLogger(PatientInterceptor.class);
    public static String status = "1";
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        boolean flag = true;
        if(Flag.close.getValue().equals(status)){
            return true;
        }
        try {
            request.setCharacterEncoding("UTF-8");
            response.setHeader("Content-type", "text/html;charset=UTF-8");
            request.setAttribute("log-start", new Date().getTime());
            response.setCharacterEncoding("UTF-8");
            JSONObject json = getAgent(request);
            logger.debug("userAgent:" + json);
            if (json == null) {
                // 未登录
                response.getOutputStream().write(error(SystemConstant.NOT_LOGIN, "请登录后再操作!").getBytes("UTF-8"));
                return false;
            }
            String tokenStr = json.has("token") ? json.getString("token") : "";
            String user = json.has("uid") ? json.getString("uid") : "";
            String imei = json.has("imei") ? json.getString("imei") : "";
            String openid = json.has("openid") ? json.getString("openid") : "";
            int platform = json.has("platform") ? json.getInt("platform") : 1;
            logger.debug("tokenStr:" + tokenStr);
            logger.debug("uid:" + user);
            logger.debug("imei:" + imei);
            logger.debug("openid:" + openid);
            logger.debug("platform:" + platform);
            if (StringUtils.isEmpty(imei)) {
                imei = openid;
            }
            Token token = null;
            if (platform == 3) {
                token = SystemData.patientTokens.get(user);
            }
            if (token == null) {
                token = tokenDao.findByPatient(user, platform);
                // 加入缓存
                if (platform == 3) {
                    SystemData.patientTokens.put(user, token);
                }
            }
            if (token == null || StringUtils.isEmpty(tokenStr) || (token.getPlatform() != 1 && token.getPlatform() != 3)) {
                // 未登录
                response.getOutputStream().write(error(SystemConstant.NOT_LOGIN, "请登录后再操作!").getBytes("UTF-8"));
                flag = false;
            } else {
                if (!StringUtils.equals(tokenStr, token.getToken()) || !StringUtils.equals(user, token.getUser()) || !StringUtils.equals(imei, token.getImei())) {
                    // 别处登录
                    response.getOutputStream().write(error(SystemConstant.LOGIN_OTHER, "帐号在别处登录,请重新登录").getBytes("UTF-8"));
                    flag = false;
                } else {
                    // 一天只更新一次
                    if (DateUtil.getDays(token.getCzrq(), DateUtil.getNowDateShort()) != 0) {
                        // 今天未更新,则更新缓存
                        token.setCzrq(new Date());
                        // 更新内存
                        if (platform == 3) {
                            SystemData.patientTokens.put(user, token);
                        }
                        // 更新数据库
                        tokenDao.save(token);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        if (null == request.getAttribute("log-start")) {
            return;
        }
        long start = (long) request.getAttribute("log-start");
        long end = new Date().getTime();
        Class cls = ((HandlerMethod) handler).getBeanType();
        RequestMapping clsRm = (RequestMapping) cls.getAnnotation(RequestMapping.class);
        Method method = ((HandlerMethod) handler).getMethod();
        RequestMapping mthRm = method.getAnnotation(RequestMapping.class);
        String url = "";
        String urlCls = "";
        String urlMth = "";
        if (clsRm.value() != null && clsRm.value().length > 0) {
            urlCls = clsRm.value()[0];
        }
        if (mthRm.value() != null && mthRm.value().length > 0) {
            urlMth = mthRm.value()[0];
        }
        if (StringUtils.isNotEmpty(urlCls)) {
            url += urlCls.startsWith("/") ? urlCls : ("/" + urlCls);
        }
        if (StringUtils.isNotEmpty(urlMth)) {
            url += urlMth.startsWith("/") ? urlMth : ("/" + urlMth);
        }
        url = url.replace("\\", "/").replace("//", "/");
        JSONObject json = getAgent(request);
        String uid = json.has("uid") ? json.getString("uid") : "";
        InterfaceCallLogs.info(end - start, url, uid, new JSONObject(request.getParameterMap()).toString());
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }
    public  String getStatus() {
        return status;
    }
    @Value("${interceptor.patient.status}")
    public  void setStatus(String status) {
        PatientInterceptor.status = status;
    }
}

+ 0 - 12
svr/svr-patient/src/main/resources/application.yml

@ -103,10 +103,6 @@ sms:
  clientId: EwC0iRSrcP
  clientId: EwC0iRSrcP
myFamily:
myFamily:
  qrCodeFailurTime: 2
  qrCodeFailurTime: 2
##拦截器开关
interceptor:
  patient:
    status: 0 ###  1开启 0 关闭
---
---
spring:
spring:
  profiles: jwtest
  profiles: jwtest
@ -138,10 +134,6 @@ sms:
  clientId: EwC0iRSrcP
  clientId: EwC0iRSrcP
myFamily:
myFamily:
  qrCodeFailurTime: 2
  qrCodeFailurTime: 2
##拦截器开关
interceptor:
  patient:
    status: 1 ###  1开启 0 关闭
---
---
spring:
spring:
  profiles: prod
  profiles: prod
@ -170,7 +162,3 @@ myFamily:
  qrCodeFailurTime: 2
  qrCodeFailurTime: 2
sms:
sms:
  clientId: EwC0iRSrcP # todo 待确认
  clientId: EwC0iRSrcP # todo 待确认
##拦截器开关
interceptor:
  patient:
    status: 1 ###  1开启 0 关闭