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

相关文章

SpringBoot3.4配置校验新特性的用法详解

《SpringBoot3.4配置校验新特性的用法详解》SpringBoot3.4对配置校验支持进行了全面升级,这篇文章为大家详细介绍了一下它们的具体使用,文中的示例代码讲解详细,感兴趣的小伙伴可以参考... 目录基本用法示例定义配置类配置 application.yml注入使用嵌套对象与集合元素深度校验开发

SpringBoot整合OpenFeign的完整指南

《SpringBoot整合OpenFeign的完整指南》OpenFeign是由Netflix开发的一个声明式Web服务客户端,它使得编写HTTP客户端变得更加简单,本文为大家介绍了SpringBoot... 目录什么是OpenFeign环境准备创建 Spring Boot 项目添加依赖启用 OpenFeig

IntelliJ IDEA 中配置 Spring MVC 环境的详细步骤及问题解决

《IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决》:本文主要介绍IntelliJIDEA中配置SpringMVC环境的详细步骤及问题解决,本文分步骤结合实例给大... 目录步骤 1:创建 Maven Web 项目步骤 2:添加 Spring MVC 依赖1、保存后执行2、将新的依赖

SpringBoot中配置文件的加载顺序解读

《SpringBoot中配置文件的加载顺序解读》:本文主要介绍SpringBoot中配置文件的加载顺序,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot配置文件的加载顺序1、命令⾏参数2、Java系统属性3、操作系统环境变量5、项目【外部】的ap

SpringBoot请求参数接收控制指南分享

《SpringBoot请求参数接收控制指南分享》:本文主要介绍SpringBoot请求参数接收控制指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring Boot 请求参数接收控制指南1. 概述2. 有注解时参数接收方式对比3. 无注解时接收参数默认位置

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾

Spring Boot读取配置文件的五种方式小结

《SpringBoot读取配置文件的五种方式小结》SpringBoot提供了灵活多样的方式来读取配置文件,这篇文章为大家介绍了5种常见的读取方式,文中的示例代码简洁易懂,大家可以根据自己的需要进... 目录1. 配置文件位置与加载顺序2. 读取配置文件的方式汇总方式一:使用 @Value 注解读取配置方式二

如何为Yarn配置国内源的详细教程

《如何为Yarn配置国内源的详细教程》在使用Yarn进行项目开发时,由于网络原因,直接使用官方源可能会导致下载速度慢或连接失败,配置国内源可以显著提高包的下载速度和稳定性,本文将详细介绍如何为Yarn... 目录一、查询当前使用的镜像源二、设置国内源1. 设置为淘宝镜像源2. 设置为其他国内源三、还原为官方

Spring 请求之传递 JSON 数据的操作方法

《Spring请求之传递JSON数据的操作方法》JSON就是一种数据格式,有自己的格式和语法,使用文本表示一个对象或数组的信息,因此JSON本质是字符串,主要负责在不同的语言中数据传递和交换,这... 目录jsON 概念JSON 语法JSON 的语法JSON 的两种结构JSON 字符串和 Java 对象互转

CentOS7更改默认SSH端口与配置指南

《CentOS7更改默认SSH端口与配置指南》SSH是Linux服务器远程管理的核心工具,其默认监听端口为22,由于端口22众所周知,这也使得服务器容易受到自动化扫描和暴力破解攻击,本文将系统性地介绍... 目录引言为什么要更改 SSH 默认端口?步骤详解:如何更改 Centos 7 的 SSH 默认端口1