大网数据广告活动
服务器运维问答

HAProxy read a cookie from request and re-assign it as a custom header

· 52CE 我爱测

I am receiving a JWT token as part of the Cookie from an auth service. Cookie: "jwt_token=eyBGfdr..................." Now, I need to read that cookie within HAProxy, extract the jwt_token key and add a custom header called jwt_to…

问题原文

原帖链接 · summerNight · CC BY-SA 4.0

I am receiving a JWT token as part of the Cookie from an auth service.

Cookie: "jwt_token=eyBGfdr..................."

Now, I need to read that cookie within HAProxy, extract the jwt_token key and add a custom header called jwt_token and assign the value eyBGfdr and finally forward the request to some other service.

I have figured out a part of the solution using the http-request set-header directive but I am not sure how to read the cookie and store it in a variable to use with the set-header directive.

Additional Explanation: My haproxy sits between an auth server and a web service. The auth server creates a JWT token and inserts that as a cookie. But, the web service on the other side can only read JWT using custom headers. Hence, I am trying to make my haproxy work in a way such that it can intercept the JWT token from the cookie and put it in the custom header so that the web service can also read it.

已采纳解答

原帖链接 · nuster cache server · CC BY-SA 4.0

haproxy.conf

global
    debug
frontend web1
    bind *:8080
    mode http
    default_backend app1
backend app1
    mode http
    http-request set-header jwt %[req.cook(jwt_token)]
    server s1 127.0.0.1:8000

haproxy version

Nuster version 1.8.8.2.2
Copyright (C) 2017-2018, Jiang Wenyuan, <koubunen AT gmail DOT com >

HA-Proxy version 1.8.8.2 2018/05/29
Copyright 2000-2018 Willy Tarreau <[email omitted]>

server.py

#!/usr/bin/env python

import SimpleHTTPServer
import SocketServer
import logging
import time

PORT = 8000

class GetHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):

    def do_GET(self):
        logging.error(self.headers)
        SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

Handler = GetHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)

httpd.serve_forever()

curl

curl -v http://127.0.0.1:8080/xxx --cookie "jwt_token=asdf"

*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> GET /xxx HTTP/1.1
> Host: 127.0.0.1:8080
> User-Agent: curl/7.60.0
> Accept: */*
> Cookie: jwt_token=asdf
> 
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Server: SimpleHTTP/0.6 Python/2.7.10

server.py log:

User-Agent: curl/7.60.0
Accept: */*
Cookie: jwt_token=asdf
jwt: asdf

127.0.0.1 - - [08/Jun/2018 13:30:19] "GET /xxx HTTP/1.1" 200 -

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-06-07)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。