diff --git a/spzx-common/common-service/src/main/java/com/atguigu/anno/EnableUserTokenFeignInterceptor.java b/spzx-common/common-service/src/main/java/com/atguigu/anno/EnableUserTokenFeignInterceptor.java new file mode 100644 index 0000000..cf41feb --- /dev/null +++ b/spzx-common/common-service/src/main/java/com/atguigu/anno/EnableUserTokenFeignInterceptor.java @@ -0,0 +1,17 @@ +package com.atguigu.anno; + +import com.atguigu.interceptor.feign.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; + +// com.atguigu.spzx.common.anno; +@Retention(value = RetentionPolicy.RUNTIME) +@Target(value = ElementType.TYPE) +@Import(value = UserTokenFeignInterceptor.class) +public @interface EnableUserTokenFeignInterceptor { + +} \ No newline at end of file diff --git a/spzx-common/common-service/src/main/java/com/atguigu/interceptor/UserLoginAuthInterceptor.java b/spzx-common/common-service/src/main/java/com/atguigu/interceptor/UserLoginAuthInterceptor.java index ea4059c..01b5234 100644 --- a/spzx-common/common-service/src/main/java/com/atguigu/interceptor/UserLoginAuthInterceptor.java +++ b/spzx-common/common-service/src/main/java/com/atguigu/interceptor/UserLoginAuthInterceptor.java @@ -3,7 +3,6 @@ package com.atguigu.interceptor; import com.alibaba.fastjson.JSON; import com.atguigu.context.BaseContext; import com.atguigu.spzx.model.entity.user.UserInfo; -import com.atguigu.utils.EmptyUtil; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; @@ -20,8 +19,6 @@ import org.springframework.web.servlet.HandlerInterceptor; public class UserLoginAuthInterceptor implements HandlerInterceptor { @Autowired private RedisTemplate redisTemplate; - @Autowired - private EmptyUtil emptyUtil; @Override public boolean preHandle(HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Object handler) throws Exception { diff --git a/spzx-common/common-service/src/main/java/com/atguigu/interceptor/feign/UserTokenFeignInterceptor.java b/spzx-common/common-service/src/main/java/com/atguigu/interceptor/feign/UserTokenFeignInterceptor.java new file mode 100644 index 0000000..b2f67c9 --- /dev/null +++ b/spzx-common/common-service/src/main/java/com/atguigu/interceptor/feign/UserTokenFeignInterceptor.java @@ -0,0 +1,18 @@ +package com.atguigu.interceptor.feign; + +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/spzx-service/service-cart/src/main/java/com/atguigu/cart/CartApplication.java b/spzx-service/service-cart/src/main/java/com/atguigu/cart/CartApplication.java index 06dfce8..c4d1393 100644 --- a/spzx-service/service-cart/src/main/java/com/atguigu/cart/CartApplication.java +++ b/spzx-service/service-cart/src/main/java/com/atguigu/cart/CartApplication.java @@ -7,8 +7,8 @@ import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.ComponentScan; // 排除数据库的自动化配置,Cart微服务不需要访问数据库 -@ComponentScan("com.atguigu") @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) +@ComponentScan("com.atguigu") @EnableFeignClients(basePackages = {"com.atguigu"}) public class CartApplication { public static void main(String[] args) { diff --git a/spzx-service/service-cart/src/main/java/com/atguigu/cart/service/impl/CartServiceImpl.java b/spzx-service/service-cart/src/main/java/com/atguigu/cart/service/impl/CartServiceImpl.java index 8e4d39b..a1bd0b4 100644 --- a/spzx-service/service-cart/src/main/java/com/atguigu/cart/service/impl/CartServiceImpl.java +++ b/spzx-service/service-cart/src/main/java/com/atguigu/cart/service/impl/CartServiceImpl.java @@ -174,7 +174,7 @@ public class CartServiceImpl implements CartService { if (!CollectionUtils.isEmpty(objectList)) { return objectList.stream() - .map(object -> JSON.parseObject(object.toString(), CartInfo.class)) + .map(object -> JSON.parseObject(JSON.toJSONString(object), CartInfo.class)) .filter(cartInfo -> cartInfo.getIsChecked() == 1) .collect(Collectors.toList()); } diff --git a/spzx-service/service-order/src/main/resources/application-dev.yml b/spzx-service/service-order/src/main/resources/application-dev.yml index cd85024..0486e73 100644 --- a/spzx-service/service-order/src/main/resources/application-dev.yml +++ b/spzx-service/service-order/src/main/resources/application-dev.yml @@ -1,4 +1,9 @@ bunny: + nacos: + server-addr: z-bunny.cn:8848 + discovery: + namespace: spzx + datasource: host: 106.15.251.123 port: 3305