|
|
@@ -1,107 +0,0 @@
|
|
|
-package com.fs.company.aspectj;
|
|
|
-
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.fs.aicall.utils.StringUtils;
|
|
|
-import com.fs.common.annotation.CallbackIpCheck;
|
|
|
-import com.fs.common.utils.IpUtil;
|
|
|
-import com.fs.company.mapper.CompanyConfigMapper;
|
|
|
-import com.fs.company.util.IpCheckUtil;
|
|
|
-import com.fs.company.vo.CidConfigVO;
|
|
|
-import com.fs.system.domain.SysConfig;
|
|
|
-import com.fs.system.mapper.SysConfigMapper;
|
|
|
-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.aspectj.lang.reflect.MethodSignature;
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-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 java.lang.reflect.Method;
|
|
|
-
|
|
|
-/**
|
|
|
- * 回调IP校验切面
|
|
|
- * <p>
|
|
|
- * 拦截所有标注了 @CallbackIpCheck 的方法,自动完成:
|
|
|
- * 1. 从方法参数中提取 companyId
|
|
|
- * 2. 查询 company_config 获取配置(如 cId.config)
|
|
|
- * 3. 解析 CidPhoneConfig 获取 legalIPs
|
|
|
- * 4. 校验请求来源IP是否在合法列表中
|
|
|
- * 5. 不合法则阻断请求
|
|
|
- * </p>
|
|
|
- *
|
|
|
- * @author MixLiu
|
|
|
- */
|
|
|
-@Aspect
|
|
|
-@Component
|
|
|
-public class CallbackIpCheckAspect {
|
|
|
-
|
|
|
- private static final Logger log = LoggerFactory.getLogger(CallbackIpCheckAspect.class);
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private CompanyConfigMapper companyConfigMapper;
|
|
|
- @Autowired
|
|
|
- private SysConfigMapper sysConfigMapper;
|
|
|
-
|
|
|
- @Pointcut("@annotation(com.fs.common.annotation.CallbackIpCheck)")
|
|
|
- public void ipCheckPointCut() {
|
|
|
- }
|
|
|
-
|
|
|
- @Around("ipCheckPointCut()")
|
|
|
- public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
|
|
|
- // 获取注解
|
|
|
- MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
|
|
- Method method = signature.getMethod();
|
|
|
- CallbackIpCheck annotation = method.getAnnotation(CallbackIpCheck.class);
|
|
|
-
|
|
|
- // 获取当前请求
|
|
|
- ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
- if (attributes == null) {
|
|
|
- throw new IllegalStateException("CallbackIpCheck: 无法获取当前请求上下文");
|
|
|
- }
|
|
|
- HttpServletRequest request = attributes.getRequest();
|
|
|
- String clientIp = IpUtil.getRequestIp(request);
|
|
|
-
|
|
|
- // 查询配置
|
|
|
- String configKey = annotation.configKey();
|
|
|
- SysConfig sysConfig = sysConfigMapper.selectConfigByConfigKey(configKey);
|
|
|
- if(null == sysConfig || StringUtils.isBlank(sysConfig.getConfigValue())){
|
|
|
- log.error("CallbackIpCheck: 未找到配置, configKey={}, 请求IP: {}",
|
|
|
- configKey, clientIp);
|
|
|
- throw new IllegalArgumentException("CallbackIpCheck: 未找到公司配置");
|
|
|
- }
|
|
|
-
|
|
|
- CidConfigVO cidConf;
|
|
|
- try {
|
|
|
- cidConf = JSONObject.parseObject(sysConfig.getConfigValue(), CidConfigVO.class);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("CallbackIpCheck: 配置JSON解析失败, configValue={}",
|
|
|
- sysConfig.getConfigValue(), e);
|
|
|
- throw new IllegalArgumentException("CallbackIpCheck: 配置解析异常");
|
|
|
- }
|
|
|
-
|
|
|
- String legalIPs = cidConf.getLegalIPs();
|
|
|
-
|
|
|
- // 校验IP
|
|
|
- if (!IpCheckUtil.isIpInList(clientIp, legalIPs)) {
|
|
|
- log.warn("非法回调来源IP: {}, legalIPs: {}", clientIp, legalIPs);
|
|
|
- // 根据目标方法的返回类型返回对应的错误响应
|
|
|
- Class<?> returnType = method.getReturnType();
|
|
|
- if (returnType == String.class) {
|
|
|
- return "illegal IP";
|
|
|
- }
|
|
|
- // 非String返回类型则抛异常,由全局异常处理器处理
|
|
|
- throw new SecurityException("非法IP来源,请求IP: " + clientIp);
|
|
|
- }
|
|
|
-
|
|
|
- // IP校验通过,放行
|
|
|
- return joinPoint.proceed();
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-}
|