OAuth2AuthorizationFilter implements ContainerRequestFilter { @Override public void filter(final ContainerRequestContext requestContext) throws IOException { SecurityContext context = requestContext.getSecurityContext(); if (context == null) { throw new OAuth2ForbiddenException(requestContext.getUriInfo(), scopesAllowed); } long matchedScopes = Arrays.stream(scopesAllowed) .filter(context::isUserInRole) .count(); if (matchedScopes == 0) { throw new OAuth2ForbiddenException(requestContext.getUriInfo(), scopesAllowed); } } OAuth2AuthorizationFilter();  OAuth2AuthorizationFilter(final String[] scopesAllowed); @Override void filter(final ContainerRequestContext requestContext);  }
@Test public void assertValidScopes() throws Exception { OAuth2AuthorizationFilter filter = new OAuth2AuthorizationFilter(new String[]{"valid"}); filter.filter(requestContext); }
@Test(expected = OAuth2ForbiddenException.class) public void assertResourceWithNoScopes() throws Exception { OAuth2AuthorizationFilter filter = new OAuth2AuthorizationFilter(); filter.filter(requestContext); }
@Test(expected = OAuth2ForbiddenException.class) public void assertNoSecurityContext() throws Exception { doReturn(null).when(requestContext).getSecurityContext(); OAuth2AuthorizationFilter filter = new OAuth2AuthorizationFilter(); filter.filter(requestContext); }
@Test(expected = OAuth2ForbiddenException.class) public void assertWithMismatchedScopes() throws Exception { OAuth2AuthorizationFilter filter = new OAuth2AuthorizationFilter(new String[]{"invalid"}); filter.filter(requestContext); }