我正在尝试使用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"}}这样的东西,但是我可以调用它,甚至不需要对它进行身份验证。
我是从lein new compojure-api my-api开始这个项目的
谢谢你的帮助!
发布于 2020-02-25 18:43:41
您可以添加提供基本身份验证的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?))https://stackoverflow.com/questions/60377068
复制相似问题