【问题标题】:How to implement security Authorization using scala and play?如何使用scala和play实现安全授权?
【发布时间】:2014-11-24 22:01:16
【问题描述】:

我正在使用 scala 和 play 框架。我想在我的应用中使用播放安全授权。

之前我在项目中使用 java 实现它并播放如下:

public class Secured extends Security.Authenticator {
    private static String EMAIL = "Email";
  private static String U_COOKIE = "ucookie";
    public String getUsername(Context ctx) {
        String decodedText = null;
        String CHARSET = "ISO-8859-1";
        Cookies cookies = play.mvc.Controller.request().cookies();
        try {
            Cookie emailCookie = cookies.get(EMAIL);
      Cookie uCookie = cookies.get(U_COOKIE);
      if (uCookie !=null && uCookie.value() != null) {
    String userId = uCookie.value();
      }
            if (emailCookie != null && emailCookie.value() != null) {
                String email = emailCookie.value();
                try {
                    decodedText = new String(Base64.decodeBase64(email.getBytes(CHARSET)));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            Logger.error(e.getMessage());
        }
        return decodedText;
    }

    public Result onUnauthorized(Context ctx) {
        String done = play.mvc.Controller.request().path();
        return redirect(routes.RegController.signIn(done));
    }
}

我在所有使用方法中都使用了上述授权

@Security.Authenticated(Secured.class)

在我的应用程序中的任何方法之前。

当我调用任何方法时,@before 该方法调用安全类并验证用户。

现在我想用 scala 来实现同样的事情。以下是我的问题....

1) 是否可以使用@来继承和调用安全类的方法??

2) 调用play的安全认证的正确方法是什么??

附:我想使用 cookie 来实现安全认证/授权。

任何帮助或解决方法都将是很大的帮助..

【问题讨论】:

    标签: scala security authentication


    【解决方案1】:

    如果您构建一个用于生产的应用程序: 不要这样做

    使用众多框架之一:

    它们也是寻找最佳实践的绝佳起点。

    如果您想主要用于学习并且没有真正的安全问题,请选择:

    https://www.playframework.com/documentation/2.3.x/ScalaActionsComposition

    寻找标题 auth 它提供了一些信息如何做到这一点。

    要在任何方法之前启动身份验证,您可以使用 Filter 拦截请求:

    https://www.playframework.com/documentation/2.3.x/ScalaInterceptors

    【讨论】:

    • 你最喜欢上面提到的哪个框架,为什么?对于实施简单的身份验证,您有什么建议?
    • 目前 Play 最新版本似乎存在一些兼容性问题。
    • 目前没有偏好。 Deadbolt 似乎是一种选择,因为它甚至可能成为游戏的一部分。
    • 在我的情况下,手动操作非常容易,因为我只需要一个带有电子邮件和密码的用户表。为什么我不应该用手做呢?你确定Deadbolt2这么受欢迎吗?最后一次提交是 2 年前。
    • 看看这里:playframework.com/documentation/2.3.x/Modules 这些是官方支持的模块。 Deadbolt 是列表的一部分。
    猜你喜欢
    • 1970-01-01
    • 2020-07-14
    • 2015-08-20
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 2020-04-10
    相关资源
    最近更新 更多