diff --git a/common/common-service/pom.xml b/common/common-service/pom.xml new file mode 100644 index 0000000..9256325 --- /dev/null +++ b/common/common-service/pom.xml @@ -0,0 +1,37 @@ + + 4.0.0 + + cn.bunny + common + 0.0.1-SNAPSHOT + + + common-service + jar + + common-service + https://maven.apache.org + + + UTF-8 + + + + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + + org.springframework.cloud + spring-cloud-loadbalancer + + + jakarta.servlet + jakarta.servlet-api + 6.1.0-M2 + + + diff --git a/common/common-service/src/main/java/cn/bunny/common/service/annotaion/EnableUserTokenFeignInterceptor.java b/common/common-service/src/main/java/cn/bunny/common/service/annotaion/EnableUserTokenFeignInterceptor.java new file mode 100644 index 0000000..a24ba5b --- /dev/null +++ b/common/common-service/src/main/java/cn/bunny/common/service/annotaion/EnableUserTokenFeignInterceptor.java @@ -0,0 +1,16 @@ +package cn.bunny.common.service.annotaion; + +import cn.bunny.common.service.interceptor.UserTokenFeignInterceptor; +import org.springframework.context.annotation.Import; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(value = RetentionPolicy.RUNTIME) +@Target(value = ElementType.TYPE) +@Import(value = UserTokenFeignInterceptor.class) +public @interface EnableUserTokenFeignInterceptor { + +} \ No newline at end of file diff --git a/common/common-service/src/main/java/cn/bunny/common/service/interceptor/UserTokenFeignInterceptor.java b/common/common-service/src/main/java/cn/bunny/common/service/interceptor/UserTokenFeignInterceptor.java new file mode 100644 index 0000000..20026cb --- /dev/null +++ b/common/common-service/src/main/java/cn/bunny/common/service/interceptor/UserTokenFeignInterceptor.java @@ -0,0 +1,21 @@ +package cn.bunny.common.service.interceptor; + +import feign.RequestInterceptor; +import feign.RequestTemplate; +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +/** + * 传递请求头,在微服务中 + */ +public class UserTokenFeignInterceptor implements RequestInterceptor { + @Override + public void apply(RequestTemplate requestTemplate) { + ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + assert requestAttributes != null; + HttpServletRequest request = requestAttributes.getRequest(); + String token = request.getHeader("token"); + requestTemplate.header("token", token); + } +} \ No newline at end of file diff --git a/common/pom.xml b/common/pom.xml index 19d4c57..54331e5 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -15,6 +15,7 @@ service-utils common-generator common-utils + common-service