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中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)、

深度解析Spring Boot拦截器Interceptor与过滤器Filter的区别与实战指南

《深度解析SpringBoot拦截器Interceptor与过滤器Filter的区别与实战指南》本文深度解析SpringBoot中拦截器与过滤器的区别,涵盖执行顺序、依赖关系、异常处理等核心差异,并... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现

springboot项目打jar制作成镜像并指定配置文件位置方式

《springboot项目打jar制作成镜像并指定配置文件位置方式》:本文主要介绍springboot项目打jar制作成镜像并指定配置文件位置方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录一、上传jar到服务器二、编写dockerfile三、新建对应配置文件所存放的数据卷目录四、将配置文

Linux如何快速检查服务器的硬件配置和性能指标

《Linux如何快速检查服务器的硬件配置和性能指标》在运维和开发工作中,我们经常需要快速检查Linux服务器的硬件配置和性能指标,本文将以CentOS为例,介绍如何通过命令行快速获取这些关键信息,... 目录引言一、查询CPU核心数编程(几C?)1. 使用 nproc(最简单)2. 使用 lscpu(详细信

Redis 配置文件使用建议redis.conf 从入门到实战

《Redis配置文件使用建议redis.conf从入门到实战》Redis配置方式包括配置文件、命令行参数、运行时CONFIG命令,支持动态修改参数及持久化,常用项涉及端口、绑定、内存策略等,版本8... 目录一、Redis.conf 是什么?二、命令行方式传参(适用于测试)三、运行时动态修改配置(不重启服务

Nginx 重写与重定向配置方法

《Nginx重写与重定向配置方法》Nginx重写与重定向区别:重写修改路径(客户端无感知),重定向跳转新URL(客户端感知),try_files检查文件/目录存在性,return301直接返回永久重... 目录一.try_files指令二.return指令三.rewrite指令区分重写与重定向重写: 请求

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

Python打印对象所有属性和值的方法小结

《Python打印对象所有属性和值的方法小结》在Python开发过程中,调试代码时经常需要查看对象的当前状态,也就是对象的所有属性和对应的值,然而,Python并没有像PHP的print_r那样直接提... 目录python中打印对象所有属性和值的方法实现步骤1. 使用vars()和pprint()2. 使