How to do it...

  1. First, we define some roles in a separate class so that we can reuse it:
public class Roles {    public static final String ROLE1 = "role1";    public static final String ROLE2 = "role2";    public static final String ROLE3 = "role3";}
  1. Then we define some things that the application's users can do:
@Statefulpublic class UserActivity {        @RolesAllowed({Roles.ROLE1})    public void role1Allowed(){        System.out.println("role1Allowed executed");    }        @RolesAllowed({Roles.ROLE2})    public void role2Allowed(){        System.out.println("role2Allowed executed");    }    @RolesAllowed({Roles.ROLE3})    public void role3Allowed(){        System.out.println("role3Allowed executed");    }    @PermitAll    public void anonymousAllowed(){ System.out.println("anonymousAllowed executed"); ...

Get Java EE 8 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.