4

Clojure サーバーのミドルウェアに問題があります。私のアプリには次の要件があります。

  • 一部のルートは問題なくアクセスできるはずです。他のものは基本認証を必要とするため、すべてのハンドラー関数の前に位置し、要求が検証されることを確認する認証関数が必要です。これには ring-basic-authentication ハンドラーを使用してきました。特に、パブリック ルートとプライベート ルートを分離する方法についての説明です。

  • ただし、Authorization:ヘッダーで送信されたパラメーターをルートコントローラーで使用できるようにしたいです。このために、リクエストのディクショナリに変数を配置する Compojure のsite関数を使用してきました(たとえば、 Compojure POST リクエストでフォーム パラメータが見つからないを参照してください) 。compojure.handler:params

ただし、401 認証とパラメーターの両方を同時に機能させることはできないようです。私がこれを試してみると:

; this is a stripped down sample case:

(defn authenticated?
  "authenticate the request"
  [service-name token]
  (:valid (model/valid-service-and-token service-name token)))

(defroutes token-routes
  (POST "/api/:service-name/phone" request (add-phone request)))

(defroutes public-routes
  controller/routes
  ; match anything in the static dir at resources/public
  (route/resources "/"))

(defroutes authviasms-handler
  public-routes
  (auth/wrap-basic-authentication 
             controller/token-routes authenticated?))

;handler is compojure.handler
(def application (handler/site authviasms-handler))

(defn start [port]
  (ring/run-jetty (var application) {:port (or port 8000) :join? false}))

認証変数はauthenticated?関数内でアクセスできますが、ルートではアクセスできません。

明らかに、これは非常に一般的な例ではありませんが、私は本当に自分の車輪を回転させ、ミドルウェアの順序をランダムに変更し、うまくいくことを望んでいるように感じます. 私の特定の例と、物事を正しく実行するためにミドルウェアをラップする方法の詳細について、いくつかの助けをいただければ幸いです。

ありがとう、ケビン

4

1 に答える 1

0

私の知る限り、 ring.middleware.basic-authentication はリクエストの :params から何も読み取らず、 ring.core.request/site も認証関連のものをそこに置きません。

ただし、どのリング ハンドラーでもヘッダーにアクセスできます。何かのようなもの:

(GET "/hello" 
  {params :params headers :headers} 
  (str "your authentication is " (headers "authentication") 
       " and you provided " params))

同様に、それを使用して独自のミドルウェアを作成し、本当に必要な場合は認証関連のものをパラメーターに入れることができます。

于 2011-10-13T07:23:20.107 に答える