一灰灰blog 一灰灰blog
首页
  • InfluxDB
  • MongoDB
  • MySql
  • 基础系列
  • DB系列
  • 搜索系列
  • MQ系列
  • WEB系列
  • 中间件
  • 运维
  • SpringSecurity
  • SpringCloud
  • QuickAlarm
  • QuickCrawer
  • QuickFix
  • QuickMedia
  • QuickSpi
  • QuickTask
  • 高可用
  • 分类
  • 标签
  • 归档
  • 收藏
  • 关于
GitHub (opens new window)

一灰灰blog

资深搬运工
首页
  • InfluxDB
  • MongoDB
  • MySql
  • 基础系列
  • DB系列
  • 搜索系列
  • MQ系列
  • WEB系列
  • 中间件
  • 运维
  • SpringSecurity
  • SpringCloud
  • QuickAlarm
  • QuickCrawer
  • QuickFix
  • QuickMedia
  • QuickSpi
  • QuickTask
  • 高可用
  • 分类
  • 标签
  • 归档
  • 收藏
  • 关于
GitHub (opens new window)
  • 基础系列

  • DB系列

  • 搜索系列

  • MQ系列

  • WEB系列

    • Request

    • Response

    • RestTemplate

    • WebClient

    • WebFlux

      • 【WEB系列】WebFlux之初体验
      • 【WEB系列】WebFlux静态资源配置与访问
      • 【WBE系列】WebFlux之Path参数解析与url映射
      • 【WEB系列】WebFlux之header参数解析
        • I. 项目环境
          • 1. 依赖
        • II. 请求头参数解析
          • 1. 请求头限制
          • 2. 请求头参数解析
          • 3. cookie获取
        • II. 其他
          • 0. 项目
          • 1. 一灰灰Blog
    • WebSocket

    • Web三剑客

    • 实例

    • 其他

  • 中间件

  • 运维

  • SpringSecurity

  • SpringCloud

  • Spring系列
  • WEB系列
  • WebFlux
一灰灰
2020-09-07

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

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

# 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>
1
2
3
4
5
6

# 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);
}
1
2
3
4
5
6
7
8
9
10
11
12
13

实例如下:

➜  ~ 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"}%   
1
2
3
4
5

# 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);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

测试case如下

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

# 3. cookie获取

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

/**
 * 获取cookie
 *
 * curl 'http://127.0.0.1:8080/header/cookie' --cookie 'tid=12343123;tt=abc123def'
 *
 * @param tid
 * @return
 */
@GetMapping(path = "cookie")
public Mono<String> getCookie(@CookieValue("tid") String tid) {
    return Mono.just("request cookies tid=" + tid);
}
1
2
3
4
5
6
7
8
9
10
11
12

上面的case中,标识只需要获取tid这个cookies值,其他的不care

➜  ~ curl 'http://127.0.0.1:8080/header/cookie' --cookie 'tid=12343123;tt=abc123def'
request cookies tid=12343123% 
1
2

# II. 其他

# 0. 项目

  • 工程:https://github.com/liuyueyi/spring-boot-demo (opens new window)
  • 源码: https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/223-webflux-params (opens new window)

系列博文

  • 【WBE系列】WebFlux之Path参数解析与url映射 (opens new window)

# 1. 一灰灰Blog

尽信书则不如,以上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激

下面一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛

  • 一灰灰Blog个人博客 https://blog.hhui.top (opens new window)
  • 一灰灰Blog-Spring专题博客 http://spring.hhui.top (opens new window)

一灰灰blog

编辑 (opens new window)
#WebFlux
上次更新: 2021/10/15, 19:56:22
【WBE系列】WebFlux之Path参数解析与url映射
【WEB系列】springboot + websocket初体验

← 【WBE系列】WebFlux之Path参数解析与url映射 【WEB系列】springboot + websocket初体验→

最近更新
01
【基础系列】基于maven多环境配置
04-25
02
【WEB系列】内嵌Tomcat配置Accesslog日志文件生成位置源码探索
04-24
03
【搜索系列】ES查询常用实例演示
04-18
更多文章>
Theme by Vdoing | Copyright © 2017-2022 一灰灰Blog
MIT License | 鄂ICP备18017282号 |
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×