在Play2 SecureSocial上,POST在SecureadAction后作为GET执行

时间:2014-01-05 12:47:33

标签: scala playframework playframework-2.1 securesocial

我正在使用PlayFramework 2.1.4和SecureSocial 2.1.1。

我在下面定义了routes,将请求设置为POST

POST    /postComment                                  controllers.Application.postComment

首先进展顺利,但在SecuredAction之后,请求更改为GET。 日志:

[info] application - onRouteRequest() requestHander = POST /postComment
[debug] application - [securesocial] anonymous user trying to access : '/postComment'
[debug] application - [securesocial] assets controller = controllers.ReverseAssets
[info] application - onRouteRequest() requestHander = GET /login
[error] application - [securesocial] can't find provider for id userpass
[info] application - onRouteRequest() requestHander = GET /authenticate/facebook
[debug] application - [securesocial] user logged in : [SocialUser(IdentityId( ...)]
[info] application - onRouteRequest() requestHander = GET /postComment
[warn] application - onHandlerNotFound() requestHander = GET /postComment

我该怎么办?请给我你的建议。

表单就像这样(createComment.scala.html)。

@helper.form(action=routes.Application.postComment){
@helper.textarea(commentForm("body"))
<div class="actions">
        <input type="submit" class="btn primary" value="submit">
</div>
}

这是Application.scala

case class CommentData(body: String, vote: String)

object Application extends Controller with SecureSocial {
val commentForm = Form(mapping("body" -> nonEmptyText)(CommentData.apply)(CommentData.unapply))
def postComment = SecuredAction { implicit request =>
val id=session.get("targetCommentId");
commentForm.bindFromRequest.fold(
formWithErrors => {
BadRequest(views.html.createComment(commentForm)).withSession(session+"targetCommentId"->id.toString)
},
commentData => {
val id = request.user.identityId.userId
val body = commentData.body
application.Application.createComment(id, body)
Ok(views.html.topiclist())
})
}
}

1 个答案:

答案 0 :(得分:2)

SecureSocial在身份验证后重定向到原始页面,但是会使用303请求其他响应,这将导致目标资源上的GET请求。虽然说你无法重定向到POST请求是一种简化,但它不适用于SecureSocial AFAIK。

处理此问题的更好方法是在用户提交评论表单之前提示验证,即:

  • anon用户想要创建评论并点击“添加评论”(或其他)
  • 通过FB进行身份验证
  • 重定向回评论表单(GET),现在已通过身份验证的用户
  • 继续处理表单提交(POST)

您可以阅读有关POST重定向here的一些问题。