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