繁体   English   中英

如何使用scala和play实现安全授权?

[英]How to implement security Authorization using scala and play?

我正在使用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的安全认证的正确方法是什么?

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

任何帮助或解决方案都将是非常有利的..

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

使用其中一个框架:

它们也是寻找最佳实践的一个很好的起点。

如果你想主要是为了学习,并且没有真正的安全问题请注意:

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM