Using programmatic security
We've already seen the declarative approach, so now let's see the programmatic approach.
Getting ready
Let's start by adding the dependency:
<dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>8.0</version> <scope>provided</scope> </dependency>
How to do it...
- Let's first define our roles list:
public class Roles { public static final String ADMIN = "admin"; public static final String USER = "user"; }
- Then, let's define a list of tasks to be done based on the role:
@Stateful public class UserBean { @RolesAllowed({Roles.ADMIN}) public void adminOperation(){ System.out.println("adminOperation executed"); } @RolesAllowed({Roles.USER}) public void userOperation(){ System.out.println("userOperation executed"); } @PermitAll public void everyoneCanDo(){ System...