相关文章推荐
闯红灯的毛巾  ·  如何自行开发代码访问阿里语音服务_智能语音交 ...·  2 月前    · 
苦闷的手术刀  ·  How to append rows ...·  1 月前    · 
绅士的书包  ·  403 Forbidden - HTTP ...·  1 月前    · 
读研的橡皮擦  ·  为什么word2016会出现单独一个文档显示 ...·  7 月前    · 
茫然的烈马  ·  sqlserver 日期格式 - CSDN文库·  8 月前    · 
爱看球的斑马  ·  Python-PyQt5-图形可视化界面(6 ...·  11 月前    · 
帅气的电影票  ·  Dockerfile ...·  1 年前    · 
怕老婆的鸡蛋  ·  GLM: ...·  1 年前    · 
Code  ›  【SpringBoot WebFlux 系列】 header 参数解析开发者社区
cookie header
https://cloud.tencent.com/developer/article/1695868
讲道义的牛肉面
1 年前
作者头像
一灰灰blog
0 篇文章

【SpringBoot WebFlux 系列】 header 参数解析

原创
前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > 小灰灰 > 【SpringBoot WebFlux 系列】 header 参数解析

【SpringBoot WebFlux 系列】 header 参数解析

原创
作者头像
一灰灰blog
修改 于 2020-09-11 10:16:41
545 0
修改 于 2020-09-11 10:16:41
举报

【SpringBoot WebFlux 系列】WebFlux 之 header 参数解析

上一篇 weblfux 主要介绍了 path 参数的解析与映射关系,在我们进入 url 参数/post 表单之前,先看一下另外的一种参数--请求头中的参数如何处理

<!-- more -->

I. 项目环境

本项目借助 SpringBoot 2.2.1.RELEASE + maven 3.5.3 + IDEA 进行开发

1. 依赖

使用 WebFlux,最主要的引入依赖如下(省略掉了 SpringBoot 的相关依赖,如对于如何创建 SpringBoot 项目不太清楚的小伙伴,可以关注一下我之前的博文)

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
</dependencies>

II. 请求头参数解析

在实际的业务开发中,有几个请求头出现的频率特别高,如常用于反爬的 User-Agent ,鉴定强求来源的 referer ,跨域相关的 Access-Control-Allow- ,cookie、session 自定义的请求头等

1. 请求头限制

在 RequestMapping 或 GetMapping 中指定请求头参数时,表示只有请求中包含这个请求头才会匹配过去

/**
 * 只有请求头包含 myheader 且值为 myvalue的才可以访问到
 * - 正常访问: curl 'http://127.0.0.1:8080/header/filter/yihhui' -H 'myheader: myvalue'
 * - 异常访问: curl 'http://127.0.0.1:8080/header/filter/yihhui' -H 'myheader: myvalue2'  因为请求头不匹配,404
 * @param name
 * @return
@GetMapping(path = "/filter/{name}", headers = "myheader=myvalue")
public Mono<String> headerFilter(@PathVariable(name = "name") String name) {
    return Mono.just("request filter: " + name);
}

实例如下:

➜  ~ curl 'http://127.0.0.1:8080/header/filter/yihhui' -H 'myheader: myvalue'
request filter: yihhui%
➜  ~ curl 'http://127.0.0.1:8080/header/filter/yihhui' -H 'myheader: myvalue2'
{"timestamp":"2020-09-07T00:40:34.493+0000","path":"/header/filter/yihhui","status":404,"error":"Not Found","message":null,"requestId":"aa47f5a5"}%

2. 请求头参数解析

WebFlux 依然是可以通过注解 @RequestHeader 来获取对应的请求头

从使用姿势上来看,webflux 与 webmvc 并没有什么区别

/**
 * 获取请求头
 * curl 'http://127.0.0.1:8080/header/get' -H 'myheader: myvalue' -H 'user-agent: xxxxxxx'
 * @param header  注意,这个是自定义的请求头
 * @param userAgent
 * @return
@GetMapping(path = "get")
public Mono<String> getHeader(@RequestHeader("myheader") String header,
        @RequestHeader("user-agent") String userAgent) {
    return Mono.just("request headers: myheader=" + header + " userAgent=" + userAgent);
}

测试 case 如下

➜  ~ curl 'http://127.0.0.1:8080/header/get' -H 'myheader: myvalue' -H 'user-agent: xxxxxxx'
request headers: myheader=myvalue userAgent=xxxxxxx%

3. cookie 获取

利用 cookie 来标识用户身份可以说是非常普遍的场景了,我们通过专用的 CookieValue 来获取指定的 cookies 值

/**
 * 获取cookie
 * curl 'http://127.0.0.1:8080/header/cookie' --cookie 'tid=12343123;tt=abc123def'
 * @param tid
 * @return
 
推荐文章
闯红灯的毛巾  ·  如何自行开发代码访问阿里语音服务_智能语音交互(ISI)-阿里云帮助中心
2 月前
苦闷的手术刀  ·  How to append rows data on grid view to header on end event page when export data to pdf? - Microsof
1 月前
绅士的书包  ·  403 Forbidden - HTTP | MDN
1 月前
读研的橡皮擦  ·  为什么word2016会出现单独一个文档显示“内存或磁盘空间不足,所请求字体无法显示” - Microsoft Community
7 月前
茫然的烈马  ·  sqlserver 日期格式 - CSDN文库
8 月前
爱看球的斑马  ·  Python-PyQt5-图形可视化界面(6)-QTableWidget - 简书
11 月前
帅气的电影票  ·  Dockerfile entrypoint.sh里为什么要执行source ~/.bashrc? - 知乎
1 年前
怕老婆的鸡蛋  ·  GLM: 自回归空白填充的通用语言模型预训练 - 知乎
1 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号