I am trying to authenticate users with a JAX-RS filter, which seems to work so far. This is the filter in which I set the new SecurityContext:
@Provider public class AuthenticationFilter implements ContainerRequestFilter { @Override public void filter(final ContainerRequestContext requestContext) throws IOException { requestContext.setSecurityContext(new SecurityContext() { @Override public Principal getUserPrincipal() { return new Principal() { @Override public String getName() { return "Joe"; } }; } @Override public boolean isUserInRole(String string) { return false; } @Override public boolean isSecure() { return requestContext.getSecurityContext().isSecure(); } @Override public String getAuthenticationScheme() { return requestContext.getSecurityContext().getAuthenticationScheme(); } }); if (!isAuthenticated(requestContext)) { requestContext.abortWith( Response.status(Status.UNAUTHORIZED) .header(HttpHeaders.WWW_AUTHENTICATE, "Basic realm=\"Example\"") .entity("Login required.").build()); } } private boolean isAuthenticated(final ContainerRequestContext requestContext) { return requestContext.getHeaderString("authorization") != null;
The resource method is as follows:
@GET // @RolesAllowed("user") public Viewable get(@Context SecurityContext context) { System.out.println(context.getUserPrincipal().getName()); System.out.println(context.isUserInRole("user")); return new Viewable("index"); }
RolesAllowedDynamicFeature is registered as follows:
.register(RolesAllowedDynamicFeature.class)
I see the expected outputs on the console. But if I uncomment @RolesAllowed("user") , I get a Forbidden error and my SecurityContext's isUserInRole method is never called. Following the doc API , RolesAllowedDynamicFeature should call this method.
How can I use RolesAllowedDynamicFeature?
source share