![](/img/trans.png)
[英]OnTouch doesnt work where do I have to put the onTouchlistener
[英]Where do I have to put @DeclareRoles?
我基本上了解@DeclareRoles
和@RolesAllowed
的功能,但我不確定在哪里正確添加@DeclareRoles
。 我在 glassfish 4 中使用帶有 ejb 會話 bean 和 cdi 的 vaadin 應用程序進行測試。該應用程序被打包成戰爭而不是耳朵。
@DeclareRoles
沒有課:HttpServletRequest.isUserInRole()
和SessionContext.isCallerInRole()
總是返回 false。 @RolesAllowed
總是拒絕訪問。@DeclareRoles
:@RolesAllowed
和HttpServletRequest.isUserInRole()
按預期工作。 SessionContext.isCallerInRole()
總是返回 false。@DeclareRoles
:@RolesAllowed
、 HttpServletRequest.isUserInRole()
和SessionContext.isCallerInRole()
按預期工作。 即使在與@DeclareRoles
不同的會話 bean 中調用SessionContext.isCallerInRole()
我現在的問題是:
@DeclareRoles
的正確位置在哪里?SessionContext.isCallerInRole()
或@RolesAllowed
?可以在類、類的業務方法或兩者上指定方法權限。 可以在 bean 類的方法上指定方法權限,以覆蓋在整個 bean 類上指定的方法權限值。 以下注解用於指定方法權限:
@DeclareRoles注解是在 bean 類上指定的,它用於聲明可以從帶注解的類的方法中測試(例如,通過調用 isCallerInRole)的角色。 在向isCallerInRole(String roleName)
方法聲明用作參數的角色名稱時,聲明的名稱必須與參數值相同。
以下示例代碼演示了 @DeclareRoles 注釋的使用:
@DeclareRoles("BusinessAdmin")
public class Calculator {
...
}
聲明多個角色的語法如下例所示:
@DeclareRoles({"Administrator", "Manager", "Employee"})
要指定沒有角色被授權訪問應用程序中的方法,請使用 @DenyAll 注釋。 要指定任何角色的用戶有權訪問應用程序,請使用 @PermitAll 注釋。
當與@DeclareRoles 注釋結合使用時,應用程序將使用組合的安全角色集。
以下示例代碼演示了@RolesAllowed注釋的使用:
@DeclareRoles({"Administrator", "Manager", "Employee"})
public class Calculator {
@RolesAllowed("Administrator")
public void setNewRate(int rate) {
...
}
}
可以在一個類或一個或多個方法上指定此注釋。 在類上指定此注釋意味着它適用於該類的所有方法。 在方法級別指定它意味着它僅適用於該方法。
以下示例代碼演示了 @PermitAll 注釋的使用:
import javax.annotation.security.*;
@RolesAllowed("RestrictedUsers")
public class Calculator {
@RolesAllowed("Administrator")
public void setNewRate(int rate) {
//...
}
@PermitAll
public long convertCurrency(long amount) {
//...
}
}
以下示例代碼演示了 @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 () {...}
...
}
更多信息:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.