我尝试在Compojure服务器上创建服务器身份验证,与朋友进行身份验证。
设置和路由似乎工作正常;当我访问/manage
路线时,我会被重定向到/login
并可以输入我的数据;但在输入用户/通行证后,我被重定向到/login?authentication_failed=Y&username=
。
添加一些日志记录显示:credential-fn
传递了用户名和密码的空字符串,重定向和拦截POST请求显示params和查询字符串始终为空,用户名或密码键不在&# 39; t出现在任何地方。
我在lein new www <name>
项目中工作,在那里我将依赖项更新为最新版本(当然也包括lates friend dep)。目前还没有-main
,我只是使用lein ring server
运行它。
有人可以帮我找出我做错的事吗?
handler.clj
(ns authme.handler
(:require [compojure.core :refer :all]
[compojure.route :as route]
[ring.middleware.defaults :refer [wrap-defaults site-defaults]]
[ring.util.response :as resp]
[cemerick.friend :as friend]
[cemerick.friend.credentials :as creds]
[cemerick.friend.workflows :as workflows]
[authme.loginpage :refer [login login-form]]
[hiccup.page :refer [html5]]))
(def users {"usr" {:username "usr"
:password (creds/hash-bcrypt "test")
:roles #{::admin}}})
(defroutes app-routes
(GET "/" request
(html5 [:a {:href "/manage"} "Manage resources"]))
(GET "/manage" request
(friend/authenticated "Admin-page")) ;; also tried authorize as admin
(GET "/login" request
(html5
(if-let [query (request :query-string)]
(do (println query)
(html5 [:div [:h2 query [:br]]
login-form]))
login-form)))
(POST "/echo" request
(let [req-lines (clojure.string/join \newline request)]
(println req-lines)
;; ugly code just to print the map without using brains
(html5 [:ul (map #(identity [:li [:b (first %) ": "] (or (second %) "-- empty --")])
request)])))
(route/not-found "Not Found"))
(def app
(friend/authenticate app-routes
{:credential-fn (fn [sign-in-info]
(prn "--sign in:" sign-in-info)
(creds/bcrypt-credential-fn users sign-in-info))
:workflows [(workflows/interactive-form)]}))
表格(loginpage.clj)
(ns authme.loginpage)
;; my own login field
(defn login []
[:form {:action "login"
:method "POST"}
[:div
[:label "Username"]
[:input {:type "text" :name "username" :required true}]] [:br]
[:div [:label "Password"]
[:input {:type "password" :name "password" :required true}]] [:br]
[:input {:type "submit" :name "submit" :value "Login"}]])
;; copy-paste from friend-demos
(def login-form
[:div {:class "row"}
[:div {:class "columns small-12"}
[:h3 "Login"]
[:div {:class "row"}
[:form {:method "POST" :action "echo" :class "columns small-4"} ;; intercept the request for debugging
[:div "Username" [:input {:type "text" :name "username"}]]
[:div "Password" [:input {:type "password" :name "password"}]]
[:div [:input {:type "submit" :class "button" :value "Login"}]]]]]])