IPBasedAnonymousAuthenticationFilter extends AnonymousAuthenticationFilter { @Override protected Authentication createAuthentication(HttpServletRequest request) { final ArrayList<GrantedAuthority> authorities = new ArrayList<>(); authorities.add(new SimpleGrantedAuthority(Roles.ROLE_UNTRUSTED_ANONYMOUS)); if (ipWhiteListed(request.getRemoteAddr()) || forwardedForTrusted(request)) { authorities.add(new SimpleGrantedAuthority(Roles.ROLE_TRUSTED_ANONYMOUS)); } return new AnonymousAuthenticationToken("ANONYMOUS", "ANONYMOUS", authorities); } IPBasedAnonymousAuthenticationFilter();   }
@Test public void testCreateAuthenticationTrustedIp() throws Exception { trust("10.5.4.8"); final HttpServletRequest request = mockRequest("10.5.4.8"); final Authentication authentication = ipBasedAnonymousAuthenticationFilter.createAuthentication(request); assertTrue(trustedAnonymousGranted(authentication)); }
@Test public void testCreateAuthenticationTrustedIpAsForwardedIp() throws Exception { trust("10.5.4.8"); final HttpServletRequest request = mockRequest("foo_bar_ip"); when(request.getHeader("X-Forwarded-For")).thenReturn("10.5.4.8"); final Authentication authentication = ipBasedAnonymousAuthenticationFilter.createAuthentication(request); assertTrue(trustedAnonymousGranted(authentication)); }
@Test public void testCreateAuthenticationTrustedIpAsForwardedIps() throws Exception { trust("10.5.4.8"); final HttpServletRequest request = mockRequest("foo_bar_ip"); when(request.getHeader("X-Forwarded-For")).thenReturn("foo, bar, 10.5.4.8"); final Authentication authentication = ipBasedAnonymousAuthenticationFilter.createAuthentication(request); assertTrue(trustedAnonymousGranted(authentication)); }
@Test public void testCreateAuthenticationTrustedIps() throws Exception { trust("10.5.4.8, 10.5.4.9"); assertTrue(trustedAnonymousGranted(ipBasedAnonymousAuthenticationFilter.createAuthentication(mockRequest("10.5.4.8")))); assertTrue(trustedAnonymousGranted(ipBasedAnonymousAuthenticationFilter.createAuthentication(mockRequest("10.5.4.9")))); }
@Test public void testOneWildcardIps() { trust("10.5.4.*"); assertTrue(trustedAnonymousGranted(ipBasedAnonymousAuthenticationFilter.createAuthentication(mockRequest("10.5.4.8")))); assertFalse(trustedAnonymousGranted(ipBasedAnonymousAuthenticationFilter.createAuthentication(mockRequest("10.5.5.8")))); }
@Test public void testTwoWildcardIps() { trust("10.5.*.*"); assertTrue(trustedAnonymousGranted(ipBasedAnonymousAuthenticationFilter.createAuthentication(mockRequest("10.5.4.8")))); assertTrue(trustedAnonymousGranted(ipBasedAnonymousAuthenticationFilter.createAuthentication(mockRequest("10.5.5.8")))); }
@Test public void testCreateAuthenticationTrustingEveryone() throws Exception { trust("*"); final HttpServletRequest request = mockRequest("some_ip"); final Authentication authentication = ipBasedAnonymousAuthenticationFilter.createAuthentication(request); assertTrue(trustedAnonymousGranted(authentication)); }
@Test public void testCreateAuthenticationNonTrustedIp() throws Exception { trust("127.0.0.1"); final HttpServletRequest request = mockRequest("10.5.4.8"); final Authentication authentication = ipBasedAnonymousAuthenticationFilter.createAuthentication(request); assertFalse(trustedAnonymousGranted(authentication)); }