首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有基本身份验证的Composure + Swagger

具有基本身份验证的Composure + Swagger
EN

Stack Overflow用户
提问于 2020-02-24 21:34:23
回答 1查看 139关注 0票数 2

我正在尝试使用compojure和swagger创建一个web服务,并使用一个非常简单的基本身份验证。我希望身份验证是单一的,例如,"mylogin“和"mypassword”作为登录和密码。到目前为止,我所拥有的是

代码语言:javascript
复制
(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开始这个项目的

谢谢你的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-25 18:43:41

您可以添加提供基本身份验证的Ring处理程序。例如ring-basic-authentication

您可以将处理程序全局添加到您的应用程序,如下所示:

代码语言:javascript
复制
(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?))
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60377068

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档