feat(新增): 解决远程调用无法传递BaseContext中内容,使用注解方式,引入openFeign拦截器
Signed-off-by: bunny <1319900154@qq.com>
This commit is contained in:
parent
836f95edd6
commit
b41919f218
|
@ -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 {
|
||||
|
||||
}
|
|
@ -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<String, Object> redisTemplate;
|
||||
@Autowired
|
||||
private EmptyUtil emptyUtil;
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Object handler) throws Exception {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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) {
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -1,4 +1,9 @@
|
|||
bunny:
|
||||
nacos:
|
||||
server-addr: z-bunny.cn:8848
|
||||
discovery:
|
||||
namespace: spzx
|
||||
|
||||
datasource:
|
||||
host: 106.15.251.123
|
||||
port: 3305
|
||||
|
|
Loading…
Reference in New Issue