OpenFeign请求拦截器,注入配置属性类(@ConfigurationProperties),添加配置文件(yml)中的token到请求头

本文主要是介绍OpenFeign请求拦截器,注入配置属性类(@ConfigurationProperties),添加配置文件(yml)中的token到请求头,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、需求

OpenFeign请求拦截器,注入配置属性类(@ConfigurationProperties),添加配置文件(yml)中的token到请求头

在使用Spring Boot结合OpenFeign进行微服务间调用时,需要在发起HTTP请求时添加一些默认的请求头,比如认证令牌(token)。为了实现这一功能,可以创建一个请求拦截器,并且通过@ConfigurationProperties来注入从配置文件中读取的属性值。

在这里插入图片描述

二、解决方案

1. 定义配置属性类

首先,定义一个Java类来封装配置文件中的属性,并使用 @ConfigurationProperties 注解来将配置文件中的值绑定到该类的实例上。

package com.example.hello_feign_client.core.property;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Data
@Component
@ConfigurationProperties(prefix = "security")
public class SecurityProperties {private String token;
}

在上面的例子中,prefix = "security"意味着配置文件(如application.propertiesapplication.yml)中,所有以 security 开头的键都会映射到这个类的属性上;token属性位于 security 键下。

2. 在配置文件中定义token

接下来,在application.yml中定义相应的属性。

security:token: 123456

3. 请求拦截器-注入配置属性类

创建请求拦截器,注入配置属性类(SecurityProperties)的实例,并从中获取token值。

package com.example.hello_feign_client.feign.config;import com.example.hello_feign_client.core.property.SecurityProperties;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;@Slf4j
@RequiredArgsConstructor
public class SecurityRequestInterceptor implements RequestInterceptor {private final SecurityProperties securityProperties;@Overridepublic void apply(RequestTemplate requestTemplate) {log.info("token={}", securityProperties.getToken());requestTemplate.header("token", securityProperties.getToken());}}

4. 配置Feign客户端

Feign客户端配置对应请求拦截器。

package com.example.hello_feign_client.feign.client;import com.example.hello_feign_client.feign.config.SecurityRequestInterceptor;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;@FeignClient(contextId = "securityFeignClient",name = "hello-feign-server",url = "http://localhost:9001",path = "/security",configuration = SecurityRequestInterceptor.class
)
public interface SecurityFeignClient {@GetMapping("/message")String getMessage();}

三、测试

运行应用程序,访问客户端 /security/message 接口,验证发送请求时,将token写入请求头;并且服务端也收到了正确的token。

客户端发送请求

客户端通过FeignClient调用接口,发送请求。

package com.example.hello_feign_client.web.security.controller;import com.example.hello_feign_client.feign.client.SecurityFeignClient;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/security")
@RequiredArgsConstructor
public class SecurityController {private final SecurityFeignClient securityFeignClient;@GetMapping("/message")public String getMessage() {return securityFeignClient.getMessage();}}

服务端接收请求

服务端接受请求,验证请求头中的token。本例中省略验证步骤,仅做日志打印。

package com.example.hello_feign_server.web.controller;import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@Slf4j
@RestController
@RequestMapping("/security")
@RequiredArgsConstructor
public class SecurityController {@GetMapping("/message")public String getMessage(@RequestHeader("token") String token) {log.info("token={}", token);return "获取到的消息";}}

接口调用成功

在这里插入图片描述

打印日志

客户端:

2024-09-05T20:42:28.915+08:00 INFO 8024 — [hello-feign-client] [nio-9002-exec-2] c.e.h.f.c.SecurityRequestInterceptor : token=123456

服务端:

2024-09-05T20:42:28.915+08:00 INFO 11188 — [hello-feign-server] [nio-9001-exec-2] c.e.h.web.controller.SecurityController : token=123456

这篇关于OpenFeign请求拦截器,注入配置属性类(@ConfigurationProperties),添加配置文件(yml)中的token到请求头的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1141678

相关文章

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.

Linux下进程的CPU配置与线程绑定过程

《Linux下进程的CPU配置与线程绑定过程》本文介绍Linux系统中基于进程和线程的CPU配置方法,通过taskset命令和pthread库调整亲和力,将进程/线程绑定到特定CPU核心以优化资源分配... 目录1 基于进程的CPU配置1.1 对CPU亲和力的配置1.2 绑定进程到指定CPU核上运行2 基于

Spring Boot spring-boot-maven-plugin 参数配置详解(最新推荐)

《SpringBootspring-boot-maven-plugin参数配置详解(最新推荐)》文章介绍了SpringBootMaven插件的5个核心目标(repackage、run、start... 目录一 spring-boot-maven-plugin 插件的5个Goals二 应用场景1 重新打包应用

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

Jenkins分布式集群配置方式

《Jenkins分布式集群配置方式》:本文主要介绍Jenkins分布式集群配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装jenkins2.配置集群总结Jenkins是一个开源项目,它提供了一个容易使用的持续集成系统,并且提供了大量的plugin满

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

Linux中SSH服务配置的全面指南

《Linux中SSH服务配置的全面指南》作为网络安全工程师,SSH(SecureShell)服务的安全配置是我们日常工作中不可忽视的重要环节,本文将从基础配置到高级安全加固,全面解析SSH服务的各项参... 目录概述基础配置详解端口与监听设置主机密钥配置认证机制强化禁用密码认证禁止root直接登录实现双因素

python删除xml中的w:ascii属性的步骤

《python删除xml中的w:ascii属性的步骤》使用xml.etree.ElementTree删除WordXML中w:ascii属性,需注册命名空间并定位rFonts元素,通过del操作删除属... 可以使用python的XML.etree.ElementTree模块通过以下步骤删除XML中的w:as

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、