Clojure、Compojure api:访问请求头

Clojure、Compojure api:访问请求头,clojure,ring,compojure-api,Clojure,Ring,Compojure Api,我正在尝试实现请求端点身份验证。为此,我希望从请求头访问accessToken值 我的GET请求结束点是 CURL命令 curl -X GET \ 'http://localhost:3000/hello?id=10' \ -H 'accesskey: 23423sfsdfsdfsfg' \ -H 'cache-control: no-cache' \ -H 'content-type: application/json' \ -H 'postman-token: f69b3

我正在尝试实现请求端点身份验证。为此,我希望从请求头访问accessToken值

我的GET请求结束点是

CURL命令

curl -X GET \
  'http://localhost:3000/hello?id=10' \
  -H 'accesskey: 23423sfsdfsdfsfg' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -H 'postman-token: f69b34e6-4888-ec31-5fbc-b734e176571b' \
  -d '{
    "artwork": {id" : 1}
}'
GET /hello?id=10 HTTP/1.1
Host: localhost:3000
Content-Type: application/json
accessKey: 23423sfsdfsdfsfg
Cache-Control: no-cache
Postman-Token: b974719d-5e1d-4d68-e910-e9ca50562b2f
HTTP命令

curl -X GET \
  'http://localhost:3000/hello?id=10' \
  -H 'accesskey: 23423sfsdfsdfsfg' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -H 'postman-token: f69b34e6-4888-ec31-5fbc-b734e176571b' \
  -d '{
    "artwork": {id" : 1}
}'
GET /hello?id=10 HTTP/1.1
Host: localhost:3000
Content-Type: application/json
accessKey: 23423sfsdfsdfsfg
Cache-Control: no-cache
Postman-Token: b974719d-5e1d-4d68-e910-e9ca50562b2f
我的GET方法实现代码

(defapi app
  (GET ["/hello/:id", :id #"[0-9]+" ] [id]
    (log/info "Function begins from here")
    (def artworkData (logic/artwork-id (->> id (re-find #"\d+") Long/parseLong)))
    (def data (if (not-empty artworkData)
               {:data artworkData :status 200}
               {:data [] :status 201}))
   (ok data)))
我想从请求头获取
accessKey:23423sfsdfsdfsfg

在我的get方法中是否有任何方法可以获取值并使用它


我正在使用POSTMAN测试所有API端点。

Compojure为参数提供了自定义的解构语法(即与Clojure本身不同)。您可以使用关键字
:as

(defapi app
  (GET ["/hello/:id", :id #"[0-9]+" ] [id :as request]
如果您只需要请求头,那么以下操作应该可以

(defapi app
  (GET ["/hello/:id", :id #"[0-9]+" ] [id :as {:headers headers}]

注意,这仍然允许您绑定路径参数
id

我已经找到了问题的解决方案。请在这里检查解决方案

(ns clojure-dauble-business-api.core
  (:require [compojure.api.sweet :refer :all]
            [ring.util.http-response :refer :all]
            [clojure-dauble-business-api.logic :as logic]
            [clojure.tools.logging :as log]
            [clojure-dauble-business-api.domain.artwork]
            [cheshire.core :as json])
  (:import [clojure_dauble_business_api.domain.artwork Artwork]))

(defapi app
  (GET ["/hello/:id", :id #"[0-9]+"] [id :as request]
    (log/info "Function begins from here" request)
    (def jsonString (json/generate-string (get-in request [:headers])))
    (log/info "Create - Access Key  is " (get-in (json/parse-string jsonString true) [:accesskey]))
    (def artworkData (logic/artwork-id (->> id (re-find #"\d+") Long/parseLong)))
    (def data (if (not-empty artworkData)
               {:data artworkData :status 200}
               {:data [] :status 201})))
我认为这不是一种明智的方式


任何人都可以查看我的解决方案并告诉我还有其他获取accesskey的方法吗?

Compojure Sweet API函数,如
[Compojure.API.Sweet:Reference[defroutes get PUT context]]
让我们绑定整个请求或绑定选择头。在下面的代码片段中,
[:as request]
使整个请求对我可用

  (GET
    "/download/:id"
    [:as request]
    :header-params [{x-http-request-id :- X-Http-Request-Id nil}]
    :path-params [id :- (describe String "The encoded id of the image")]
    :summary "Download the image bytes"
    :description "This endpoint responds 307 - Temporary Redirect to a cacheable presigned S3 URL for the actual bytes."
    (let [http-response (->> request
                             walk/keywordize-keys
                             util/extract-base-url
                             (transform/generate-resource-url (util/decode-key id))
                             status/temporary-redirect)
          expire-time (-> 3 hours from-now coerce/to-date ring-time/format-date)]
      (log/infof "x-http-request-id is %s" x-http-request-id)
      (response/header http-response "Expires" expire-time)))
  • 向量开头的
    :头参数[{x-http-request-id:-x-http-request-id nil}]
    使请求中的“x-http-request-id”头的值直接作为
    x-http-request-id
    可供我的函数使用
  • squiglies thing
    {…}
    使得请求中的x-http-request-id头的存在是可选的
  • :-X-Http-Request-Id nil
    东西为它提供了一个在其他地方定义的模式,如
    (s/defschema X-Http-Request-Id(rss/descripe字符串“跟踪调用的请求Id”)

  • 一旦你把那些孩子和名字联系在一起,你就可以用这些名字了。compojure的人在记录你能在那里做的每件事方面做得并不好。翻开他们的例子,你会发现类似的东西。

    谢谢你的快速帮助。我已经发布了我问题的答案。如果你正在建造token/JWT/,你能调查一下吗?授权,可能首先看看Ring的现成授权模块,比如。我认为您已经回答了“访问请求头”的原始问题。如果您没有提供与其他(本例中为我的)答案基本相同的内容,我也将不胜感激。@Srini如果它足够聪明,我不会称之为“不聪明的方式”。获取头值的较短方法是通过
    :header params
    机制。像访问令牌这样的东西通常是通过环中的中间件完成的,因为您通常需要它跨多条路由的逻辑。