【问题标题】:Cemerick Friend & Single Page AppCemerick 朋友和单页应用程序
【发布时间】:2014-09-14 01:36:09
【问题描述】:

我正在使用 angular 和 clojure 构建一个单页应用程序。目前,用户通过 html 模板中的表单提供和提交其凭据来注册他们的帐户并登录。 Clojure 然后检查他们的密码是否与数据库中的加密副本匹配,并返回一个包含他们的用户名、名字/姓氏、角色和其他一些信息的 json 对象。整个前端是棱角分明的,因此,有一条主要路线:

(defroutes main
  (GET "/" [] (layout/master)))

应用程序向其他功能的路由服务发出各种请求:

(defroutes service
  (GET "/api/private" {params :params} (http/json-response 200 {:success true :message "it worked"}))
  (GET "/api/even-privater" {params :params} (http/json-response 200 {:success true :message "it really worked"})))

目前,这些路线上没有安全措施。我想要的是使用friend/authenticate 来保护这些服务路线,但是,我似乎找不到适合我的工作流程,也找不到任何有关将 cemerick/friend 用于单页应用程序的文档。

理想情况下,用户会登录,然后能够向安全路由发出请求。如果他们没有通过身份验证,他们只会收到 401 响应。成功登录后,用户将收到带有一些相关用户信息的 http 200。

我已经通读了workflows 代码、this stackoverflow post 以及this issue solution,但仍然无法完全理解我需要做什么。 here 找到的演示帮助我了解朋友可以做什么,但我发现很难应用我从他们那里学到的东西。

理想情况下,我想将应用程序放在一起,如下所示:

(def secured-service
  (friend/authenticate
   routes/service
   {:credential-fn a-credential-function
    :unauthenticated-handler {:status 401 :body "Unauthenticated"}
    :workflows [(workflows/a-spa-workflow)]}))

(def app (middleware/app-handler
                  [routes/main routes/public secured-service routes/app]
                  :middleware []
                  :formats [:json-kw :edn]))

通过使用make-auth的路由处理身份验证

(defroutes public
  (POST "/api/login" {params :params} (workflows/make-auth user-record ...)))

有谁知道有关此问题的一些文档可以帮助我吗?或者,更好的是,关于如何实现这一点的任何想法?

【问题讨论】:

  • 这似乎更多的是服务器问题而不是角度问题 - 我已经像这样jonsamwell.com/url-route-authorization-and-security-in-angular 完成了我的前端角度安全性
  • 这是一个很棒的设置,尽管我认为它与我的问题无关。我的目标是在用户未通过后端身份验证的情况下防止对 Clojure 服务路由的 http 请求通过。我也有安全性(好吧,显示/隐藏是我们可以从角度做的所有事情)基于前端使用拦截器的角色 - 我需要的不是前端的安全性,而是后端的安全性以保护路由实际上与我的数据库交谈并需要授权/身份验证。

标签: angularjs clojure


【解决方案1】:

我设法使用friend json workflow 解决了我自己的问题。我的代码最终看起来像这样:

(def api-handler
  (-> routes/api
      wrap-keyword-params
      wrap-nested-params
      wrap-params))

(def app
  (middleware/app-handler
   [routes/main routes/public routes/api routes/app]
   :middleware []
   :formats [:json-kw :edn]))

(def secure-app
  (-> app
      (friend/authenticate {:login-uri "/api/session"
                            :unauthorized-handler json-auth/unauthorized-handler
                            :workflows [(json-auth/json-login
                                         :login-uri "/api/session"
                                         :login-failure-handler json-auth/login-failed
                                         :credential-fn (partial creds/bcrypt-credential-fn session/get-user))]})
      (ring-session/wrap-session)))

我的路线(routes/mainroutes/publicroutes/apiroutes/app)在哪里:

(defroutes main
  (GET "/" [] (layout/master)))

(defroutes public
  (GET "/api/session" [] json-auth/handle-session)
  (POST "/api/session" {params :params} json-auth/handle-session)
  (DELETE "/api/session" [] json-auth/handle-session)
  (POST "/api/register" {params :params} (auth/register (:credentials params) (:name params))))

(defroutes api
  (GET "/api/private" {params :params} 
       (friend/authenticated (http/json-response 200 {:success true :message "it worked"})))
  (GET "/api/even-privater" {params :params} 
       (friend/authenticated (http/json-response 200 {:success true :message "it really worked"}))))

(defroutes app
  (route/resources "/")
  (route/not-found "Not Found"))

我现在通过 POST 到 /api/session 进行身份验证,通过 DELETE 注销到 /api/session,并通过 GET 到 /api/session 获取当前会话。

【讨论】:

  • 能否请您发布您的 :use 和 :require ?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-26
  • 1970-01-01
  • 2016-02-10
  • 2015-07-29
  • 2012-08-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多