I basically understand the functions of @DeclareRoles
and @RolesAllowed
, but I'm not sure where to add the @DeclareRoles
correctly. I testet with a vaadin application with ejb session beans and cdi in glassfish 4. The application is packed as war not as ear.
@DeclareRoles
on no class: HttpServletRequest.isUserInRole()
and SessionContext.isCallerInRole()
are always returning false. @RolesAllowed
always denies access.@DeclareRoles
on the Servlet: @RolesAllowed
and HttpServletRequest.isUserInRole()
are working as expected. SessionContext.isCallerInRole()
is always returning false.@DeclareRoles
on a session bean: @RolesAllowed
, HttpServletRequest.isUserInRole()
and SessionContext.isCallerInRole()
are working as expected. Even if SessionContext.isCallerInRole()
is called in a different session bean than the one with @DeclareRoles
My question now are:
@DeclareRoles
?SessionContext.isCallerInRole()
or @RolesAllowed
?Method permissions can be specified on the class, the business methods of the class, or both. Method permissions can be specified on a method of the bean class to override the method permissions value specified on the entire bean class. The following annotations are used to specify method permissions:
The @DeclareRoles annotation is specified on a bean class, where it serves to declare roles that can be tested (for example, by calling isCallerInRole) from within the methods of the annotated class. When declaring the name of a role used as a parameter to the isCallerInRole(String roleName)
method, the declared name must be the same as the parameter value.
The following example code demonstrates the use of the @DeclareRoles annotation:
@DeclareRoles("BusinessAdmin")
public class Calculator {
...
}
The syntax for declaring more than one role is as shown in the following example:
@DeclareRoles({"Administrator", "Manager", "Employee"})
To specify that no roles are authorized to access methods in an application, use the @DenyAll annotation. To specify that a user in any role is authorized to access the application, use the @PermitAll annotation.
When used in conjunction with the @DeclareRoles annotation, the combined set of security roles is used by the application.
The following example code demonstrates the use of the @RolesAllowed annotation:
@DeclareRoles({"Administrator", "Manager", "Employee"})
public class Calculator {
@RolesAllowed("Administrator")
public void setNewRate(int rate) {
...
}
}
This annotation can be specified on a class or on one or more methods. Specifying this annotation on the class means that it applies to all methods of the class. Specifying it at the method level means that it applies to only that method.
The following example code demonstrates the use of the @PermitAll annotation:
import javax.annotation.security.*;
@RolesAllowed("RestrictedUsers")
public class Calculator {
@RolesAllowed("Administrator")
public void setNewRate(int rate) {
//...
}
@PermitAll
public long convertCurrency(long amount) {
//...
}
}
The following example code demonstrates the use of the @DenyAll annotation:
import javax.annotation.security.*;
@RolesAllowed("Users")
public class Calculator {
@RolesAllowed("Administrator")
public void setNewRate(int rate) {
//...
}
@DenyAll
public long convertCurrency(long amount) {
//...
}
}
The following code snippet demonstrates the use of the @DeclareRoles annotation with the isCallerInRole method. In this example, the @DeclareRoles annotation declares a role that the enterprise bean PayrollBean uses to make the security check by using isCallerInRole("payroll")
to verify that the caller is authorized to change salary data:
@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(...);
}
...
}
...
}
The following example code illustrates the use of the @RolesAllowed annotation:
@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 () {...}
...
}
More information:
Securing Enterprise Beans
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With