package com.ycl.interceptor; import com.alibaba.fastjson2.JSONObject; import com.ycl.feign.UYClient; import com.ycl.platform.domain.param.UY.UYTokenParam; import com.ycl.platform.domain.result.UY.UYLoginResult; import com.ycl.utils.redis.RedisCache; import feign.RequestInterceptor; import feign.RequestTemplate; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Lazy; import org.springframework.data.redis.core.RedisTemplate; import java.util.concurrent.TimeUnit; /** * 优云请求拦截器 */ @Slf4j public class YYFeignInterceptor implements RequestInterceptor { @Autowired @Lazy private UYClient uyClient; @Autowired private RedisCache redisCache; @Value("${youYun.email}") private String email; @Value("${youYun.passwd}") private String passwd; @Override public void apply(RequestTemplate requestTemplate) { boolean flag = requestTemplate.headers().containsKey("No-Need-To-Token"); if (!flag) { String uyToken = redisCache.getCacheObject("UY_Token"); if (uyToken == null) { uyToken = getToken(); } requestTemplate.header("Cookie", "token=" + uyToken); } } private String getToken() { UYTokenParam param = new UYTokenParam(); param.setPasswd(passwd); param.setEmail(email); JSONObject jsonObject = uyClient.getToken(param); UYLoginResult loginResult = jsonObject.getObject("data", UYLoginResult.class); String token = loginResult.getToken(); redisCache.setCacheObject("Uy_Token", token, 3600, TimeUnit.SECONDS); return token; } }