当前位置: 首页 > news >正文

代做网站 作业搜狗搜索引擎优化论文

代做网站 作业,搜狗搜索引擎优化论文,武汉网站制作模板,网站建设的目标SpringBoot 微服务中怎么获取用户信息 token 当我们写了一个A接口,这个接口需要调用B接口,但是B接口需要包含请求头内容,比如需要用户信息、用户id等内容,由于不在同一个线程中,使用ThreadLocal去获取数据是无法获取的…

SpringBoot 微服务中怎么获取用户信息 token

当我们写了一个A接口,这个接口需要调用B接口,但是B接口需要包含请求头内容,比如需要用户信息、用户id等内容,由于不在同一个线程中,使用ThreadLocal去获取数据是无法获取的,这个时候需要手动将信息放置请求头中。

前置知识

获取请求头方式

return BaseContext.getUserId();是我随便写的不要在意!!!

第一种 ServletRequestAttributes

通过ServletRequestAttributes获取请求。

@GetMapping("/token")
public Long getToken() {ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();HttpServletRequest request = requestAttributes.getRequest();String token = request.getHeader("token");return BaseContext.getUserId();
}

第二种 HttpServletRequest

@GetMapping("/token")
public Long getToken(HttpServletRequest request) {String token = request.getHeader("token");return BaseContext.getUserId();
}

第二种 通过注解

@GetMapping("/token")
public Long getToken(@RequestHeader("token") String token) {return BaseContext.getUserId();
}

操作步骤

准备好两个接口还有一个OpenFeign。

OpenFeign

@FeignClient(value = "service-admin",contextId = "IndexFeignClient", path = "")
public interface IndexFeignClient {@GetMapping("/token")Long getToken();
}

被调用接口B

@GetMapping("/token")
public Long getToken() {ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();HttpServletRequest request = requestAttributes.getRequest();String token = request.getHeader("token");return BaseContext.getUserId();
}

调用接口A

@GetMapping("abc")
public String index() {Long id = indexFeignClient.getToken();return "欢迎访问。。。";
}

这个时候正常去调用这个接口是无法获取到token相关信息的。

可以看到是null,因为没有将请求头传递过来。

在这里插入图片描述

传递请求头

我使用的是JWT方式,这种可以存储一些信息。使用的是注解方式存入请求头。

重写以实现对Feign请求的拦截和处理,首先通过RequestContextHolder.getRequestAttributes()方法获取ServletRequestAttributes对象,这个对象封装了当前请求的相关信息。然后将ServletRequestAttributes对象转换为ServletRequestAttributes类型,并确保它不为空。

使用requestTemplate.header("token", token)方法将获取到的token值作为请求的header添加到Feign请求模板中。这样,在发起Feign请求时,会自动将token作为请求头一并发送给目标服务。

创建UserTokenFeignInterceptor

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 {@Overridepublic 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);}
}

使用注解

使用@Import将之前的UserTokenFeignInterceptor引入。

import cn.bunny.common.utils.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;@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = ElementType.TYPE)
@Import(value = UserTokenFeignInterceptor.class)
public @interface EnableUserTokenFeignInterceptor {}

最后在接口启动类上加上这个注解,也就是在接口A的启动类上这个注解。不要加错了!!!

是接口A的启动类!!!

是接口A的启动类!!!

是接口A的启动类!!!

@EnableFeignClients(basePackages = {"cn.bunny.feign.admin"})
@SpringBootApplication
@Slf4j
@EnableUserTokenFeignInterceptor
public class ServiceWebApplication {public static void main(String[] args) {log.info("ServiceApplication启动...");SpringApplication.run(ServiceWebApplication.class, args);}
}

之后就可以看到传递成功的token

在这里插入图片描述

http://www.hkea.cn/news/116127/

相关文章:

  • 网络推广网站怎么做百度联盟广告点击一次收益
  • wordpress居中样式宁波seo网络推广外包报价
  • java做网站用到哪些技术网络营销的重要性与意义
  • 网络营销推广的作用谷歌seo什么意思
  • 免费网站建设解决方案郑州网络营销公司哪个好
  • 转转怎么做钓鱼网站税收大数据
  • 株洲专业网站排名优化深圳产品网络推广
  • 深圳美食教学网站制作如何免费搭建自己的网站
  • 兰州移动端网站建设广东整治互联网霸王条款
  • 彩票网站该怎么建设天津seo实战培训
  • 原平的旅游网站怎么做的新冠疫情最新情况最新消息
  • 网站开发软件著作权归谁seo外包
  • 小说网站的网编具体做哪些工作南宁网站快速排名提升
  • 承德网站设计seo互联网营销培训
  • 工信部网站备案查询 手机seo专员的工作内容
  • 淘宝活动策划网站视频营销成功的案例
  • 精准营销数据杭州排名优化软件
  • 中卫网站建站设计seo学习论坛
  • wordpress初始登录seo排名赚app靠谱吗
  • 软件外包保密协议seo相关岗位
  • 后台网站开发文档下载班级优化大师app
  • 辛集城乡建设管理局网站网络营销网络推广
  • 阿里云部署一个自己做的网站吗电商网站搭建
  • 免费汽车租赁网站模板网站域名解析ip查询
  • 企业解决方案官网国内seo排名分析主要针对百度
  • 变态版手游石景山区百科seo
  • 阿里云控制台登录入口seo矩阵培训
  • wordpress苗木模板网站搜索排优化怎么做
  • 网站图片引导页怎么做重庆seo招聘
  • 如何做属于自己的领券网站郑州百度网站优化排名