gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:28:31 26 4
gpt4 key购买 nike

我正在使用 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 来实现安全认证/授权。

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

最佳答案

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

使用众多框架之一:

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

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

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

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

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

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

关于scala - 如何使用scala和play实现安全授权?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27105295/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com