簡體   English   中英

我必須把@DeclareRoles 放在哪里?

[英]Where do I have to put @DeclareRoles?

我基本上了解@DeclareRoles@RolesAllowed的功能,但我不確定在哪里正確添加@DeclareRoles 我在 glassfish 4 中使用帶有 ejb 會話 bean 和 cdi 的 vaadin 應用程序進行測試。該應用程序被打包成戰爭而不是耳朵。

  • @DeclareRoles沒有課:
    顯然沒有任何作用。 HttpServletRequest.isUserInRole()SessionContext.isCallerInRole()總是返回 false。 @RolesAllowed總是拒絕訪問。
  • Servlet 上的@DeclareRoles
    @RolesAllowedHttpServletRequest.isUserInRole()按預期工作。 SessionContext.isCallerInRole()總是返回 false。
  • 會話 bean 上的@DeclareRoles
    @RolesAllowedHttpServletRequest.isUserInRole()SessionContext.isCallerInRole()按預期工作。 即使在與@DeclareRoles不同的會話 bean 中調用SessionContext.isCallerInRole()

我現在的問題是:

  1. 放置@DeclareRoles的正確位置在哪里?
  2. 可以只設置一次還是應該注釋每個使用SessionContext.isCallerInRole()@RolesAllowed

可以在類、類的業務方法或兩者上指定方法權限。 可以在 bean 類的方法上指定方法權限,以覆蓋在整個 bean 類上指定的方法權限值。 以下注解用於指定方法權限:

  • @DeclareRoles:指定應用程序將使用的所有角色,包括未在@RolesAllowed注釋中具體命名的角色。 應用程序使用的安全角色集是@DeclareRoles 和@RolesAllowed注釋中定義的安全角色的總和

@DeclareRoles注解是在 bean 類上指定的,它用於聲明可以從帶注解的類的方法中測試(例如,通過調用 isCallerInRole)的角色。 在向isCallerInRole(String roleName)方法聲明用作參數的角色名稱時,聲明的名稱必須與參數值相同。

以下示例代碼演示了 @DeclareRoles 注釋的使用:

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

聲明多個角色的語法如下例所示:

@DeclareRoles({"Administrator", "Manager", "Employee"})
  • @RolesAllowed("list-of-roles"):指定允許訪問應用程序中方法的安全角色。 可以在一個類或一個或多個方法上指定此注釋。 在類級別指定時,注釋適用於類中的所有方法。 在方法上指定時,注釋僅適用於該方法並覆蓋在類級別指定的任何值。

要指定沒有角色被授權訪問應用程序中的方法,請使用 @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 () {...}
    ...
}

更多信息:

保護企業 Bean

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM