我应该把@DeclareRoles放在哪里?(涉及IT技术)

7
我基本了解 @DeclareRoles@RolesAllowed 的功能,但不确定应该在哪里正确地添加@DeclareRoles。我使用具有EJB会话bean和CDI的Vaadin应用程序进行测试,使用的是GlassFish 4,并且应用程序被打包为war而不是ear。
  • @DeclareRoles 不在任何类上:
    显然什么都不起作用。HttpServletRequest.isUserInRole()SessionContext.isCallerInRole() 总是返回false。@RolesAllowed 始终拒绝访问。
  • @DeclareRoles 在Servlet上:
    @RolesAllowedHttpServletRequest.isUserInRole() 正如预期的那样工作。SessionContext.isCallerInRole() 总是返回false。
  • @DeclareRoles 在会话bean上:
    @RolesAllowedHttpServletRequest.isUserInRole()SessionContext.isCallerInRole() 正如预期的那样工作。即使在具有不同@DeclareRoles 的会话bean中调用 SessionContext.isCallerInRole()也是如此。

现在我的问题是:

  1. 放置 @DeclareRoles 的正确位置是哪里?
  2. 只设置一次是否可以,还是应该注释使用 SessionContext.isCallerInRole()@RolesAllowed 的每个bean?
1个回答

4

方法权限可以在类、类的业务方法或两者上指定。可以在bean类的方法上指定方法权限,以覆盖在整个bean类上指定的方法权限值。以下注释用于指定方法权限:

  • @DeclareRoles: 指定应用程序将使用的所有角色,包括未在@RolesAllowed注释中明确命名的角色。应用程序使用的安全角色集是在@DeclareRoles和@RolesAllowed注释中定义的安全角色的总和。

在bean类上指定@DeclareRoles注释,用于声明可以从注释类的方法内部测试的角色(例如,通过调用isCallerInRole)。在将角色名称声明为作为isCallerInRole(String roleName)方法参数使用时,声明的名称必须与参数值相同。

以下示例代码演示了使用@DeclareRoles注释的情况:

@DeclareRoles("BusinessAdmin")
public class Calculator {
    ...
}

声明多个角色的语法如下所示:
@DeclareRoles({"Administrator", "Manager", "Employee"})
  • @RolesAllowed("角色列表"): 指定应用程序中允许访问方法的安全角色。可以在类上或一个或多个方法上指定此注释。当在类级别指定时,该注释适用于类中的所有方法。当在方法上指定时,该注释仅适用于该方法,并覆盖在类级别指定的任何值。

要指定没有角色被授权访问应用程序中的方法,请使用@DenyAll注释。要指定任何角色中的用户都被授权访问应用程序,请使用@PermitAll注释。

与@DeclareRoles注释一起使用时,应用程序使用安全角色的组合集。

以下示例代码演示了使用@RolesAllowed注释:

@DeclareRoles({"Administrator", "Manager", "Employee"})
public class Calculator {

    @RolesAllowed("Administrator")
    public void setNewRate(int rate) {
        ...
    }
}
  • @PermitAll:指定所有安全角色都可以执行指定的方法或方法。不会检查用户是否经过授权才能访问该应用程序。

此注释可在类或一个或多个方法上指定。在类上指定此注释意味着它适用于该类的所有方法。在方法级别上指定它意味着它仅适用于该方法。

以下示例代码演示了使用 @PermitAll 注释:

import javax.annotation.security.*;
@RolesAllowed("RestrictedUsers")
public class Calculator {

    @RolesAllowed("Administrator")
    public void setNewRate(int rate) {
        //...
    }
    @PermitAll
    public long convertCurrency(long amount) {
        //...
    }
}
  • @DenyAll:指定没有安全角色允许执行指定的方法或方法。这意味着这些方法在Java EE容器中被排除在执行范围之外。

下面的示例代码演示了如何使用@DenyAll注解:

import javax.annotation.security.*;
@RolesAllowed("Users")
public class Calculator {
    @RolesAllowed("Administrator")
    public void setNewRate(int rate) {
        //...
    }
    @DenyAll
    public long convertCurrency(long amount) {
        //...
    }
}

下面的代码片段演示了使用@DeclareRoles注释和isCallerInRole方法。在这个例子中,@DeclareRoles注释声明了一个角色,企业Bean PayrollBean使用该角色通过使用isCallerInRole("payroll")进行安全检查,以验证调用者是否有权更改薪资数据:
@DeclareRoles("payroll")
@Stateless 
public class PayrollBean implements Payroll {

    @Resource SessionContext ctx;

    public void updateEmployeeInfo(EmplInfo info) {

        oldInfo = ... read from database;

        // The salary field can be changed only by callers
        // who have the security role "payroll"
        Principal callerPrincipal = ctx.getCallerPrincipal();
        if (info.salary != oldInfo.salary && !ctx.isCallerInRole("payroll")) {
            throw new SecurityException(...);
        }
        ...
    }
    ...
}

下面的示例代码演示了如何使用@RolesAllowed注解:
@RolesAllowed("admin")
public class SomeClass {
    public void aMethod () {...}
    public void bMethod () {...}
    ...
}

@Stateless 
public class MyBean extends SomeClass implements A  {

    @RolesAllowed("HR")
    public void aMethod () {...}

    public void cMethod () {...}
    ...
}

更多信息:

保护企业 Beans


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接