Este contenido no está disponible en el idioma seleccionado.
Chapter 33. Securing JAX-RS and RESTeasy
Because RESTEasy is deployed as a Servlet, you must use standard
web.xml
constraints to enable authentication and authorization.
Unfortunately,
web.xml
constraints have limited compatibility with JAX-RS because of the limited URL pattern matching available in web.xml
. URL patterns in web.xml
support only simple wildcards, so JAX-RS resources like the following:
/{pathparam1}/foo/bar/{pathparam2}
/{pathparam1}/foo/bar/{pathparam2}
Cannot be mapped as a
web.xml
URL pattern such as:
/*/foo/bar/*
/*/foo/bar/*
To work around this problem, use the following security annotations on your JAX-RS methods. You must also set up some general security constraint elements in
web.xml
to enable authentication.
RESTEasy JAX-RS supports the
@RolesAllowed
, @PermitAll
and @DenyAll
annotations on JAX-RS methods. By default, RESTEasy does not recognize these annotations. You must configure RESTEasy to enable role-based security by setting a context parameter, like so:
Note
Do not enable this if you are using EJBs. The EJB container will provide this function instead of RESTEasy.
With this approach, you must declare all roles used within both the RESTEasy JAX-RS
WAR
file, and in your JAX-RS classes, and establish a security constraint that lets these roles access every URL handled by the JAX-RS runtime, assuming that RESTEasy authorizes correctly.
RESTEasy authorization checks if a method is annotated with
@RolesAllowed
and then performs HttpServletRequest.isUserInRole
. If one of the the @RolesAllowed
passes, the request is allowed. If not, a response is returned with a 401
(Unauthorized) response code.
The following is an example of a modified RESTEasy WAR file. Note that every role declared is allowed access to every URL controlled by the RESTEasy Servlet.