具有基本身份验证的 Composure + Swagger
Composure + Swagger with basic authentication
我正在尝试通过非常简单的基本身份验证使用 compojure 和 swagger 创建 Web 服务。我希望身份验证是单一的,例如,"mylogin" 和 "mypassword" 用于登录名和密码。到目前为止,我所拥有的是
(ns my-api.handler
(:require [compojure.api.sweet :refer :all]
[ring.util.http-response :refer :all]
[schema.core :as s]))
(s/defschema Request
{:SomeData s/Str})
(s/defschema Result
{:Results s/Str})
(defn dummy-return [request]
{:Results "This is a dummy return"})
(def app
(api
{:swagger
{:ui "/"
:spec "/swagger.json"
:data {:info {:title "A testing API"
:description "Compojure Api example"}
:tags [{:name "api", :description "some apis"}]}}}
(context "/api" []
:tags ["api"]
(POST "/API-Example" []
:body [request Request]
:return Result
:summary "I want this to have a fixed basic authentication"
(ok (dummy-return request))))))
它按预期工作。但是我如何通过一个固定用户的基本身份验证来实现相同的 API 呢?我来自 python,使用 Flask 做到这一点非常容易。我想知道是否有一个简单而优雅的解决方案,而不是太冗长。
我试图在与 :data
相同的级别中包含一些像 :securityDefinitions {:login {:type "basic" :password "test"}}
的东西,但我可以调用 API 即使没有验证它。
我用 lein new compojure-api my-api
开始了这个项目
感谢您的帮助!
您可以添加提供基本身份验证的 Ring 处理程序。例如 ring-basic-authentication.
您将全局处理程序添加到您的应用程序中,如下所示:
(require '[ring.middleware.basic-authentication :refer [wrap-basic-authentication]])
(defn authenticated? [username pass]
(and (= username "foo")
(= pass "bar")))
(def app
(-> routes
..
(wrap-basic-authentication authenticated?))
我正在尝试通过非常简单的基本身份验证使用 compojure 和 swagger 创建 Web 服务。我希望身份验证是单一的,例如,"mylogin" 和 "mypassword" 用于登录名和密码。到目前为止,我所拥有的是
(ns my-api.handler
(:require [compojure.api.sweet :refer :all]
[ring.util.http-response :refer :all]
[schema.core :as s]))
(s/defschema Request
{:SomeData s/Str})
(s/defschema Result
{:Results s/Str})
(defn dummy-return [request]
{:Results "This is a dummy return"})
(def app
(api
{:swagger
{:ui "/"
:spec "/swagger.json"
:data {:info {:title "A testing API"
:description "Compojure Api example"}
:tags [{:name "api", :description "some apis"}]}}}
(context "/api" []
:tags ["api"]
(POST "/API-Example" []
:body [request Request]
:return Result
:summary "I want this to have a fixed basic authentication"
(ok (dummy-return request))))))
它按预期工作。但是我如何通过一个固定用户的基本身份验证来实现相同的 API 呢?我来自 python,使用 Flask 做到这一点非常容易。我想知道是否有一个简单而优雅的解决方案,而不是太冗长。
我试图在与 :data
相同的级别中包含一些像 :securityDefinitions {:login {:type "basic" :password "test"}}
的东西,但我可以调用 API 即使没有验证它。
我用 lein new compojure-api my-api
感谢您的帮助!
您可以添加提供基本身份验证的 Ring 处理程序。例如 ring-basic-authentication.
您将全局处理程序添加到您的应用程序中,如下所示:
(require '[ring.middleware.basic-authentication :refer [wrap-basic-authentication]])
(defn authenticated? [username pass]
(and (= username "foo")
(= pass "bar")))
(def app
(-> routes
..
(wrap-basic-authentication authenticated?))