Authentication 播放框架处理授权而不是身份验证

Authentication 播放框架处理授权而不是身份验证,authentication,playframework,authorization,playframework-2.2,Authentication,Playframework,Authorization,Playframework 2.2,我正在用PlayFramework2.2和Java开发一个应用程序 我已经实现了身份验证模块,就像下面的教程一样 简而言之,实现了一个安全的类 public class Secured extends Security.Authenticator{ @Override public String getUsername(Context ctx) { return ctx.session().get("ema

我正在用PlayFramework2.2和Java开发一个应用程序 我已经实现了身份验证模块,就像下面的教程一样

简而言之,实现了一个安全的类

    public class Secured extends Security.Authenticator{

            @Override
            public String getUsername(Context ctx) {
               return ctx.session().get("email");
           }

           @Override
            public Result onUnauthorized(Context ctx) {
               return redirect(routes.Users.login());
           }
     }
然后在控制器中,我将这一行添加到控制器的方法中

     @Security.Authenticated(Secured.class)
     public static Result methodOfController(){

          //some codes here

    return ok( someView.render());
}
正如您所看到的,它只是身份验证而不是授权,例如,它检查用户是否登录,但从不检查这是否是管理员的电子邮件

我的问题是:我应该如何向这些类添加访问权限,或者说,我应该如何向该身份验证添加授权

请提供一个描述性的答案,说明我应该对这个类、控制器甚至项目的其他部分(可能是模型)进行哪些修改,以获得适当的授权


请不要提供指向网站或网络日志的链接,除非它们关注的是非常类似的问题

您可以查看这样的解决方案,或者您也可以推出自己的解决方案。Java的主要思想是用来创建自定义动作注释。因此,您可以检查用户是否经过身份验证,然后检查该用户是否被授权使用请求的资源。

我已经为我们的项目编写了一个简单的授权操作组合

在操作或控制器之前,可以添加一行,如下所示:

@Auth({"GeneralManager","Manager"})
使用上面的行,只有角色为“GeneralManager”或“Manager”的人员才能访问操作或控制器。“AuthAction”的实现可以如下所示:

public class AuthAction extends Action<Auth> {

public F.Promise<SimpleResult> call(Http.Context context) throws Throwable
{
    String[] params = configuration.value();
    int c = params.length;

    boolean found = false;
    if(params.length == 0) {
        found = true;
    }

   // Loop the given parameters(role names) to check that the user belongs to one of them
    for (String code: params) {
        // validate types
        int roleCount = Role.find.where().eq("code",code).findRowCount();
        if(roleCount == 0) {
            throw new Exception("Auth code is not found.");
        }

        if(user.role.code.equals(code)) {
            found = true;
        }
    }

    // if the role is not found for the user, it means the user is not authorised
    if(!found) {
        // no access, redirect to home
        return F.Promise.pure(redirect("/"));
    }

    // execute the action
    return delegate.call(context);
}
}
public类AuthAction扩展了Action{
public F.Promise调用(Http.Context上下文)抛出可丢弃的
{
字符串[]params=configuration.value();
int c=参数长度;
布尔值=false;
如果(params.length==0){
发现=真;
}
//循环给定的参数(角色名称)以检查用户是否属于其中一个
for(字符串代码:params){
//验证类型
int-roleCount=Role.find.where().eq(“code”,code).findRowCount();
如果(roleCount==0){
抛出新异常(“未找到身份验证代码”);
}
if(user.role.code.equals(code)){
发现=真;
}
}
//如果未找到该用户的角色,则表示该用户未经授权
如果(!找到){
//无法访问,请重定向到主页
返回F.Promise.pure(重定向(“/”);
}
//执行操作
返回delegate.call(上下文);
}
}

谢谢您的时间,我将应用您的解决方案,如果它有效,我会检查它是否为正确答案。如果您可以发布完整的代码,那就太好了。缺少批注身份验证的实现