LdapRoleAuthenticationFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain) throws ServletException, IOException { if (request.isUserInRole(requiredRole)) { LOG.debug("Found correct role for login."); filterChain.doFilter(request, response); } else { LOG.warn("Did not find correct role for login."); unauthorized(response); } } LdapRoleAuthenticationFilter(final LdapProperties ldapProperties);   }
@Test public void shouldRejectUserThatHasNotRequiredRole() throws ServletException, IOException { final LdapProperties ldapProperties = mockLdapPropertiesWithRequiredRole("roleX"); final LdapRoleAuthenticationFilter filter = new LdapRoleAuthenticationFilter(ldapProperties); final HttpServletRequest request = mockRequestWithAvailableRoles("roleA", "roleB"); final HttpServletResponse response = mockResponse(); final FilterChain filterChain = mockFilterChain(); filter.doFilterInternal(request, response, filterChain); verifyZeroInteractions(filterChain); verify(response).setStatus(HttpServletResponse.SC_UNAUTHORIZED); }
@Test public void shouldContinueFilterChainWhenUserHasRequiredRole() throws ServletException, IOException { final LdapProperties ldapProperties = mockLdapPropertiesWithRequiredRole("roleB"); final LdapRoleAuthenticationFilter filter = new LdapRoleAuthenticationFilter(ldapProperties); final HttpServletRequest request = mockRequestWithAvailableRoles("roleA", "roleB", "roleC"); final HttpServletResponse response = mockResponse(); final FilterChain filterChain = mockFilterChain(); filter.doFilterInternal(request, response, filterChain); verify(filterChain).doFilter(request, response); verifyZeroInteractions(response); }