教程里关于feign调用拿不到token的问题 #2244

集成xxl-job-2.2.0之后,注解没有删掉,导致启动报端口冲突 #2228
This commit is contained in:
zhangdaiscott 2021-02-17 21:54:23 +08:00
parent 53d410896a
commit dc91b8439c
4 changed files with 81 additions and 77 deletions

View File

@ -95,6 +95,9 @@
<#if po.classType=='pca'>
<#assign list_need_pca=true>
</#if>
<#if po.classType=='switch'>
<#assign list_need_switch=true>
</#if>
</#list>
<#-- 结束循环 -->
<#t>

View File

@ -0,0 +1,76 @@
package org.jeecg.config;
import feign.Feign;
import feign.Logger;
import feign.RequestInterceptor;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.CommonConstant;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
@ConditionalOnClass(Feign.class)
@AutoConfigureBefore(FeignAutoConfiguration.class)
@Slf4j
@Configuration
public class FeignConfig {
@Bean
public RequestInterceptor requestInterceptor() {
return requestTemplate -> {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (null != attributes) {
HttpServletRequest request = attributes.getRequest();
log.info("Feign request: {}", request.getRequestURI());
// 将token信息放入header中
String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
if(token==null){
token = request.getParameter("token");
}
log.info("Feign request token: {}", token);
requestTemplate.header(CommonConstant.X_ACCESS_TOKEN, token);
}
};
}
/**
* Feign 客户端的日志记录默认级别为NONE
* Logger.Level 的具体级别如下
* NONE不记录任何信息
* BASIC仅记录请求方法URL以及响应状态码和执行时间
* HEADERS除了记录 BASIC级别的信息外还会记录请求和响应的头信息
* FULL记录所有请求与响应的明细包括头信息请求体元数据
*/
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
/**
* Feign支持文件上传
* @param messageConverters
* @return
*/
@Bean
@Primary
@Scope("prototype")
public Encoder multipartFormEncoder(ObjectFactory<HttpMessageConverters> messageConverters) {
return new SpringFormEncoder(new SpringEncoder(messageConverters));
}
}

View File

@ -1,76 +0,0 @@
//package org.jeecg.starter.cloud.config;
//
//import feign.Feign;
//import feign.Logger;
//import feign.RequestInterceptor;
//import feign.codec.Encoder;
//import feign.form.spring.SpringFormEncoder;
//import lombok.extern.slf4j.Slf4j;
//import org.jeecg.common.constant.CommonConstant;
//import org.springframework.beans.factory.ObjectFactory;
//import org.springframework.boot.autoconfigure.AutoConfigureBefore;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
//import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
//import org.springframework.cloud.openfeign.FeignAutoConfiguration;
//import org.springframework.cloud.openfeign.support.SpringEncoder;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Primary;
//import org.springframework.context.annotation.Scope;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.http.HttpServletRequest;
//
//
//@ConditionalOnClass(Feign.class)
//@AutoConfigureBefore(FeignAutoConfiguration.class)
//@Slf4j
//@Configuration
//public class FeignClientConfig {
//
// @Bean
// public RequestInterceptor requestInterceptor() {
// return requestTemplate -> {
// ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
// if (null != attributes) {
// HttpServletRequest request = attributes.getRequest();
// log.info("Feign request: {}", request.getRequestURI());
// // 将token信息放入header中
// String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
// if(token==null){
// token = request.getParameter("token");
// }
// log.info("Feign request token: {}", token);
// requestTemplate.header(CommonConstant.X_ACCESS_TOKEN, token);
// }
// };
// }
//
//
//
// /**
// * Feign 客户端的日志记录默认级别为NONE
// * Logger.Level 的具体级别如下
// * NONE不记录任何信息
// * BASIC仅记录请求方法URL以及响应状态码和执行时间
// * HEADERS除了记录 BASIC级别的信息外还会记录请求和响应的头信息
// * FULL记录所有请求与响应的明细包括头信息请求体元数据
// */
// @Bean
// Logger.Level feignLoggerLevel() {
// return Logger.Level.FULL;
// }
//
// /**
// * Feign支持文件上传
// * @param messageConverters
// * @return
// */
// @Bean
// @Primary
// @Scope("prototype")
// public Encoder multipartFormEncoder(ObjectFactory<HttpMessageConverters> messageConverters) {
// return new SpringFormEncoder(new SpringEncoder(messageConverters));
// }
//}

View File

@ -25,7 +25,8 @@ public class XxlJobConfiguration {
@Autowired
private XxlJobProperties xxlJobProperties;
@Bean(initMethod = "start", destroyMethod = "destroy")
//@Bean(initMethod = "start", destroyMethod = "destroy")
@Bean
@ConditionalOnClass()
public XxlJobSpringExecutor xxlJobExecutor() {
log.info(">>>>>>>>>>> xxl-job config init.");