已经在config类中,对两个拦截器进行定义了。但是在打断点的时候,TokenInterceptor中重写的apply()方法没有被执行。
@Configuration @Slf4j public class FeginConfig { @Bean @Order(1) public TokenInterceptor tokenInterceptor(){ log.info("TokenInterceptor bean created"); return new TokenInterceptor(); } @Bean @Order(2) public MessageInterceptor messageInterceptor(){ log.info("MessageInterceptor bean created"); return new MessageInterceptor(); } }
未执行apply方法的拦截器。
@Slf4j public class TokenInterceptor implements RequestInterceptor { private String token; /** * 记录上次更新令牌时间 */ private long lastUpdateTime; private String tokenExpireTime; @Autowired private FeiShuApiService feiShuApiService; @Autowired public TenantAccessToken tenantAccessToken; @Value("${app.app_id}") private String appId; @Value("${app.app_secret}") private String appSecret; @Value("${app.token_url}") private String tokenUrl; @PostConstruct public void init(){ // 初始化时获取一次 access_token refreshAccessToken(); } /** * 该重新接口,每次请求前都会被调用一次,用于检查token * @param requestTemplate */ @Override public void apply(RequestTemplate requestTemplate) { log.info("TokenInterceptor apply method called"); //第一次获取token,token最新更新时间和过期时间为null,跳过该方法 if (lastUpdateTime != 0 || tokenExpireTime != null){ if ((System.currentTimeMillis() - lastUpdateTime) >= Long.parseLong(tokenExpireTime) * 1000) { TenantAccessToken tenantAccessToken = this.refreshAccessToken(); token = tenantAccessToken.getTenantAccessToken(); log.info("token:" + token); } requestTemplate.header("Content-Type","application/json; charset=utf-8"); requestTemplate.header("Authorization", "Bearer " + token); } } //获取token private TenantAccessToken refreshAccessToken() { // 获取新的 access_token Map<String, String> tokenResponse = feiShuApiService.getTenantAccessToken(appId,appSecret); TenantAccessToken tenantAccessToken = new TenantAccessToken(); tenantAccessToken.setCode(tokenResponse.get("code")); tenantAccessToken.setExpire(tokenResponse.get("expire")); tenantAccessToken.setTenantAccessToken(tokenResponse.get("tenant_access_token")); tenantAccessToken.setMsg(tokenResponse.get("msg")); this.tokenExpireTime = tokenResponse.get("expire"); this.token = tokenResponse.get("tenant_access_token"); this.lastUpdateTime = System.currentTimeMillis(); return tenantAccessToken; } }
正常执行apply方法的拦截器
@Order(2) public class MessageInterceptor implements RequestInterceptor { @Autowired public TenantAccessToken tenantAccessToken; @Override public void apply(RequestTemplate requestTemplate) { if (requestTemplate.url().contains("/open-apis/im/v1/messages")){ requestTemplate.header("receive_id_type","user_id"); requestTemplate.header("content-type","application/json; charset=utf-8"); requestTemplate.header("Authorization", "Bearer " + tenantAccessToken.getTenantAccessToken() ); } } }
原因:
通过@Autowired注解注入的 feiShuApiService 类使用了 @FeignClient 注解,以及 @Value 注解,这些会导致不执行 apply方法。具体原因不明。
解决办法:
删除以上注入的类和属性,寻找其他方法代替。
标签:拦截器,requestTemplate,private,token,tenantAccessToken,apply,RequestInterceptor,pub From: https://www.cnblogs.com/yanhongwen/p/18571982