【问题标题】:Proper use of @Security.Authenticated(Secured.class) statement in Play FrameWork在 Play FrameWork 中正确使用 @Security.Authenticated(Secured.class) 语句
【发布时间】:2014-01-30 18:32:51
【问题描述】:

您好,我无法理解如何正确使用 @Security.Authenticated(Secured.class) PlayFrameWork 中的语句。

我正在努力确保只有经过身份验证的用户才能访问他们的帐户。根据 Play docs 中提供的示例,他们的身份验证似乎允许单个用户在登录后访问每个用户的帐户,而不仅仅是他们自己的帐户。

通常我会假设您只是在操作中获取会话值,例如,

    public static Result viewAccount(String account) {
        //get session value and check against account name
    }

但是 Docs 使用另一条路线: 他们定义了一个类

    public class Secured extends Security.Authenticator {
        @Override
        public String getUsername(Context ctx) {
            return ctx.session().get("username");
        }

        @Override
        public Result onUnauthorized(Context ctx) {
            return redirect(routes.Application.login());
        }
    }

显然现在只需使用以下语句: @Security.Authenticated(Secured.class) 在一个动作确保它被认证之前。但是根据我的理解和测试,这不会阻止用户登录到任何人的帐户,因为它只是在会话值存在的情况下通过 - 如果它匹配则不会。 如何解决这个问题?

我应该直接比较会话值吗?那么目的是什么 @Security.Authenticated(Secured.class)?

谢谢 (编辑) 澄清: 我想让用户被授权只能查看他们自己的帐户,而不是其他人。
所以当声明

    @Security.Authenticated(Secured.class)

已使用,我希望它不仅检查会话 ID 的存在,而且检查它是否与帐户匹配

【问题讨论】:

    标签: java http session playframework


    【解决方案1】:

    也许我不明白您的问题,但在documentation 中您看到了如何创建登录表单并实际执行身份验证。只有在那之后,电子邮件会话值才存在。如果该值在会话中,则用户必须使用正确的凭据登录并且必须经过身份验证。

    当然,如果用户 A 知道用户 B 的凭据,则无济于事。

    如果您担心授权,决定谁可以通过身份验证看到什么,那么您可以做很多事情,包括利用 Play 中的 OAuth 支持或像 this one 这样的插件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-10
      • 2014-04-02
      • 1970-01-01
      • 2012-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多