在我的新项目中,我想使用webmachine和mochiweb。首先要做的事情是认证。
我编辑了“dispatch.conf”并创建了一些资源,例如:
{["auth"], my_res_auth, []}.
{["protected"], my_res_protected, []}.
{['*'], my_res_default, []}.
当有人访问“受保护”的资源时,如果他没有登录,我希望将他重定向到“认证”资源。 “认证”资源包含一个带有用户名和密码的Web表单,它完成所有身份验证工作。
我在my_res_protected.erl中放置了以下代码:
is_authorized(ReqData, State) ->
case my_auth:is_authorized(ReqData) of
true -> {true, ReqData, State};
false ->
% here should be something to redirect user to "auth" resource
% currently i put such thing, which is incorrect:
{true, wrq:do_redirect(true, wrq:set_resp_header("location", "/auth", ReqData)), State}
% dont know what should i put instead of "true"
end.
我搜索了一些关于如何做到这一点的示例,但我不喜欢必须在所有资源中放置这些需要验证的函数。是否有其他方法可以实现?
case State#state.do_redirect of
- seancribbs