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

相关文章

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤

《SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤》本文主要介绍了SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤,文中通过示例代码介绍的非常详... 目录 目标 步骤 1:确保 ProxySQL 和 mysql 主从同步已正确配置ProxySQL 的

Spring Boot整合log4j2日志配置的详细教程

《SpringBoot整合log4j2日志配置的详细教程》:本文主要介绍SpringBoot项目中整合Log4j2日志框架的步骤和配置,包括常用日志框架的比较、配置参数介绍、Log4j2配置详解... 目录前言一、常用日志框架二、配置参数介绍1. 日志级别2. 输出形式3. 日志格式3.1 PatternL

配置springboot项目动静分离打包分离lib方式

《配置springboot项目动静分离打包分离lib方式》本文介绍了如何将SpringBoot工程中的静态资源和配置文件分离出来,以减少jar包大小,方便修改配置文件,通过在jar包同级目录创建co... 目录前言1、分离配置文件原理2、pom文件配置3、使用package命令打包4、总结前言默认情况下,

修改若依框架Token的过期时间问题

《修改若依框架Token的过期时间问题》本文介绍了如何修改若依框架中Token的过期时间,通过修改`application.yml`文件中的配置来实现,默认单位为分钟,希望此经验对大家有所帮助,也欢迎... 目录修改若依框架Token的过期时间修改Token的过期时间关闭Token的过期时js间总结修改若依

在Java中使用ModelMapper简化Shapefile属性转JavaBean实战过程

《在Java中使用ModelMapper简化Shapefile属性转JavaBean实战过程》本文介绍了在Java中使用ModelMapper库简化Shapefile属性转JavaBean的过程,对比... 目录前言一、原始的处理办法1、使用Set方法来转换2、使用构造方法转换二、基于ModelMapper

VScode连接远程Linux服务器环境配置图文教程

《VScode连接远程Linux服务器环境配置图文教程》:本文主要介绍如何安装和配置VSCode,包括安装步骤、环境配置(如汉化包、远程SSH连接)、语言包安装(如C/C++插件)等,文中给出了详... 目录一、安装vscode二、环境配置1.中文汉化包2.安装remote-ssh,用于远程连接2.1安装2

JavaScript中的isTrusted属性及其应用场景详解

《JavaScript中的isTrusted属性及其应用场景详解》在现代Web开发中,JavaScript是构建交互式应用的核心语言,随着前端技术的不断发展,开发者需要处理越来越多的复杂场景,例如事件... 目录引言一、问题背景二、isTrusted 属性的来源与作用1. isTrusted 的定义2. 为

Redis多种内存淘汰策略及配置技巧分享

《Redis多种内存淘汰策略及配置技巧分享》本文介绍了Redis内存满时的淘汰机制,包括内存淘汰机制的概念,Redis提供的8种淘汰策略(如noeviction、volatile-lru等)及其适用场... 目录前言一、什么是 Redis 的内存淘汰机制?二、Redis 内存淘汰策略1. pythonnoe