How do I get the @RolesAllowed annotation to work for my Web application?

前端 未结 4 550
暗喜
暗喜 2020-12-14 18:46

I am making a Web application using Backbone.js, Bootstrap, NetBeans IDE 8.0, Java EE 7, JDK 8, WildFly server 8.1.0, JBoss RESTEasy (resteasy-jaxrs-3.0.8), JBoss 2.2.22, JB

4条回答
  •  爱一瞬间的悲伤
    2020-12-14 19:16

    I found one solution to my problem. However, this isn't as much a solution to the problem as it is a workaround, because it does not use the @RolesAllowed annotation.

    Since I was unable to figure out how to define my deployment descriptors and server configuration exactly, I figured the problem would be solved much easier if I simply did not use the @RolesAllowed annotation.

    Even though other people might really want to use the login-config element in their web.xml file and not use any other means of authentication, this approach does not use that element but instead does authentication solely through RESTful Web Services (which means that nothing needs to change in the deployment descriptors or the server configuration).

    I created a new Enterprise Java Bean (EJB) called SecurityFilter which checks if a user has the required roles for certain functionalities. It is implemented as follows:

    import java.util.Arrays;
    import java.util.HashSet;
    import java.util.Set;
    import javax.ejb.EJB;
    import javax.ejb.Stateless;
    import javax.ws.rs.core.HttpHeaders;
    import org.profit.pgb.rest.user.UserService;
    
    @Stateless
    public class SecurityFilter
    {
        @EJB(name = "UserServiceImp")
        UserService userService;
    
        public boolean isUserAllowed (String[] rolesArray, HttpHeaders hHeaders)
        {
            Set rolesSet = new HashSet<>(Arrays.asList(rolesArray));
    
            String uuid = hHeaders.getRequestHeader("user").get(0);
            String token = hHeaders.getRequestHeader("token").get(0);
    
            if (userService.isAuthorizationTokenValid(uuid, token))
            {
               if (userService.isUserAllowed(uuid, rolesSet))
               {
                   return true; // user allowed access
               }
            }   
            return false; // 401
        }
    }
    

    The method isUserAllowed is called in the create method of UserResource.java. The old implementation of this create method can be seen in the question above. The new implementation is as follows:

    @PermitAll
    @Path("create")
    @POST
    public Response create(CreateRequest request, @Context HttpHeaders hHeaders) {  
        if (securityFilter.isUserAllowed(new String[]{"admin"}, hHeaders))
        {
            try {
                System.out.println("Start of create method");
                User user = userService.createUser(request);
                return getCreateResponse(user);
            }
            catch (Exception e){
                return Response.status(401).entity("Failed to create user").build();
            }
        }
        else
            return Response.status(401).entity("Access denied! User does not have permission to create user").build();
    }
    

    As you can see, an if-else statement replaces the @RolesAllowed annotation in this approach and my security filter is implemented slightly different.

    Also, this approach uses HttpHeaders to get the request headers (in which the user ID and token are stored). The accepted answer on the SO question "how to received "Accept" header in REST web service server side" helped me find how to get the request headers.

    Furthermore, this approach works without changing anything in my Backbone.js- and Bootstrap-based web pages (i.e. my HTML and JavaScript files).

提交回复
热议问题