Cemerick Friend互动形式 - 无法识别的身体

时间:2015-01-13 17:57:44

标签: authentication clojure ring

我正在尝试使用Cemerick Friend的交互式表单工作流进行身份验证。当我发布到我的login-uri时,我收到以下错误:

2015-01-13 12:51:22.945:WARN:oejs.AbstractHttpConnection:/api/login
java.lang.Exception: Unrecognized body: {:error "Wrong credentials", :uri {:ssl-client-cert nil, :remote-addr "0:0:0:0:0:0:0:1", :params {}, :headers {"origin" "http://localhost:3000", "host" "localhost:3000", "user-agent" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 OPR/26.0.1656.60", "content-type" "application/json", "content-length" "42", "referer" "http://localhost:3000/?username=friend&password=clojure", "connection" "keep-alive", "accept" "*/*", "accept-language" "en-US,en;q=0.8", "accept-encoding" "gzip, deflate, lzma", "dnt" "1"}, :server-port 3000, :content-length 42, :form-params {}, :query-params {}, :content-type "application/json", :character-encoding nil, :uri "/api/login", :server-name "localhost", :query-string nil, :body #<HttpInput org.eclipse.jetty.server.HttpInput@1edd9ed>, :scheme :http, :cemerick.friend/auth-config {:login-failure-handler #<handler$fn__5557 evertask.core.handler$fn__5557@39741646>, :unauthorized-handler #<handler$fn__5555 evertask.core.handler$fn__5555@69677f38>, :allow-anon? true, :unauthenticated-handler #<handler$fn__5553 evertask.core.handler$fn__5553@13eecd31>, :redirect-on-auth? false, :default-landing-uri "/", :login-uri "/api/login", :credential-fn #<handler$fn__5551 evertask.core.handler$fn__5551@77e17b33>, :workflows [#<workflows$interactive_form$fn__5175 cemerick.friend.workflows$interactive_form$fn__5175@745f8ec1>]}, :request-method :post}}

我的经纪人&amp;相关代码:

(ns myproject.core.handler
  (:require [compojure.handler :as ch]
            [ring.middleware.json :as rj]
            [ring.util.response :as rr]
            [cemerick.friend :as friend]
            (cemerick.friend [workflows :as workflows]
                             [credentials :as creds])
            [myproject.core.route :as routes]))

(def users {"friend" {:username "friend"
                      :password (creds/hash-bcrypt "clojure")}})

(def friend-options {:allow-anon? true
                     :redirect-on-auth? false
                     :login-failure-handler #(-> (rr/response {:error "Wrong credentials" :uri %}) (rr/status 401))
                     :unauthenticated-handler #(-> (rr/response {:error "Unauthenticated" :uri %}) (rr/status 401))
                     :login-uri "/api/login"
                     :default-landing-uri "/"
                     :unauthorized-handler #(-> (rr/response {:error "Unauthorized" :uri %}) (rr/status 401))
                     :credential-fn #(creds/bcrypt-credential-fn users %)
                     :workflows [(workflows/interactive-form :redirect-on-auth? false)]})

(def secured-app
  (-> (ch/api routes/api)
      (rj/wrap-json-body :keywords? true)
      (rj/wrap-json-response)
      (friend/authenticate friend-options)))

ClojureScript触发登录:

(ns myproject.services.auth                                                                                                                                    
  (:require-macros [cljs.core.async.macros :refer [go]])
  (:require [cljs-http.client :as http]     
            [cljs.core.async :refer [<! >! chan put!]]
            [myproject.core.store :as store]))

(defn login [res username password]                  
  (go (let [result (<! (http/post "/api/login" {:json-params {:username username :password password}}))]
        (.log js/console (pr-str result))   
        (if (= (:status result) 200)
          (let [access (:body result)]
            (swap! store/app-state assoc :access access)
            (swap! store/app-state assoc :view :tasks)
            (>! res true))     
          (>! res false)))))   

(defn active-access [res]      
  (go (let [access (<! (http/get "/api/session"))]   
        (if (= (:status access) 200)        
          (>! res (:body access))            
          (>! res false)))))

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

原来问题是我的中间件的顺序。我需要在 wrap-json-body之后friend-authenticate