package com.lantone.qc.security.aop;

import com.lantone.qc.pub.model.vo.TokenVO;
import com.lantone.qc.security.exception.CommonErrorCode;
import com.lantone.qc.security.exception.CommonException;
import com.lantone.qc.security.util.TokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

/**
 * @Description: 安全验证切面
 * @author: gaodm
 * @time: 2019/12/24 10:23
 */
@Aspect
@Component
@Slf4j
@ConditionalOnProperty(prefix = "mysecurity", value = { "enable" }, havingValue = "true")
public class SecurityAop {

    @Pointcut("@annotation(com.lantone.qc.security.aop.Security)")
    public void securityPointCut() {

    }

    @Before("securityPointCut()")
    public void SecurityCheck(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        TokenVO tokenVO = null;
        for (Object obj : args) {
            // 获取token参数
            tokenVO = (TokenVO)obj;
        }
        boolean pass = TokenUtil.verifyToken(tokenVO);
        if (!pass) {
            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "接口校验未通过，请联系朗通!");
        }
    }
}
