ShiroJwtVerifyingFilter extends AccessControlFilter { @Override protected boolean isAccessAllowed(ServletRequest req, ServletResponse res, Object mappedValue) throws Exception { Subject nonJwt = getSubject(req, res); return null != nonJwt.getPrincipal() && (nonJwt.isRemembered() || nonJwt.isAuthenticated()) ? true : getJwtUser(req).isPresent(); } ShiroJwtVerifyingFilter();   }
@Test public void testIsRemembered() throws Exception { try { Subject subject = Mockito.mock(Subject.class); Mockito.when(subject.getPrincipal()).thenReturn(Mockito.mock(Object.class)); Mockito.when(subject.isRemembered()).thenReturn(true); ThreadContext.bind(subject); ShiroJwtVerifyingFilter filter = new ShiroJwtVerifyingFilter(); Assertions.assertThat( filter.isAccessAllowed( Mockito.mock(HttpServletRequest.class), Mockito.mock(ServletResponse.class), Mockito.mock(Object.class))) .isTrue(); } finally { ThreadContext.unbindSubject(); } }
@Test public void testIsAuthenticated() throws Exception { try { Subject subject = Mockito.mock(Subject.class); Mockito.when(subject.getPrincipal()).thenReturn(Mockito.mock(Object.class)); Mockito.when(subject.isAuthenticated()).thenReturn(true); ThreadContext.bind(subject); ShiroJwtVerifyingFilter filter = new ShiroJwtVerifyingFilter(); Assertions.assertThat( filter.isAccessAllowed( Mockito.mock(HttpServletRequest.class), Mockito.mock(ServletResponse.class), Mockito.mock(Object.class))) .isTrue(); } finally { ThreadContext.unbindSubject(); } }
@Test public void testNewRequest() throws Exception { ShiroJwtVerifyingFilter filter = new ShiroJwtVerifyingFilter(); Assertions.assertThat( filter.isAccessAllowed( Mockito.mock(HttpServletRequest.class), Mockito.mock(ServletResponse.class), Mockito.mock(Object.class))) .isFalse(); }
@Test public void testAuthorization0() throws Exception { try { SecurityUtils.setSecurityManager(new DefaultSecurityManager()); new ShiroJwtProvider(Mockito.mock(AppContext.class)); HttpServletRequest req = Mockito.mock(HttpServletRequest.class); Mockito.when(req.getHeader("Authorization")).thenReturn("junk"); ShiroJwtVerifyingFilter filter = new ShiroJwtVerifyingFilter(); Assertions.assertThat( filter.isAccessAllowed( req, Mockito.mock(ServletResponse.class), Mockito.mock(Object.class))) .isFalse(); } finally { ThreadContext.unbindSubject(); ThreadContext.unbindSecurityManager(); } }
@Test public void testAuthorization1() throws Exception { try { SecurityUtils.setSecurityManager(new DefaultSecurityManager()); new ShiroJwtProvider(Mockito.mock(AppContext.class)); HttpServletRequest req = Mockito.mock(HttpServletRequest.class); Mockito.when(req.getHeader("Authorization")).thenReturn("Bearer "); ShiroJwtVerifyingFilter filter = new ShiroJwtVerifyingFilter(); Assertions.assertThat( filter.isAccessAllowed( req, Mockito.mock(ServletResponse.class), Mockito.mock(Object.class))) .isFalse(); } finally { ThreadContext.unbindSubject(); ThreadContext.unbindSecurityManager(); } }
@Test public void testAuthorization2() throws Exception { try { SecurityUtils.setSecurityManager(new DefaultSecurityManager()); new ShiroJwtProvider(Mockito.mock(AppContext.class)); HttpServletRequest req = Mockito.mock(HttpServletRequest.class); Mockito.when(req.getHeader("Authorization")).thenReturn("Bearer eyJhbGciOiJIUzI1NiJ9"); ShiroJwtVerifyingFilter filter = new ShiroJwtVerifyingFilter(); Assertions.assertThat( filter.isAccessAllowed( req, Mockito.mock(ServletResponse.class), Mockito.mock(Object.class))) .isFalse(); } finally { ThreadContext.unbindSubject(); ThreadContext.unbindSecurityManager(); } }
@Test public void testAuthorization3() throws Exception { try { SecurityUtils.setSecurityManager(new DefaultSecurityManager()); new ShiroJwtProvider(Mockito.mock(AppContext.class)); HttpServletRequest req = Mockito.mock(HttpServletRequest.class); Mockito .when(req.getHeader("Authorization")) .thenReturn( "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0LXVzZXIifQ.neIA5mbTFZsZokqG5CFwK7gIxMiBoGOU0anDZmD7kkU"); ShiroJwtVerifyingFilter filter = new ShiroJwtVerifyingFilter(); Assertions.assertThat( filter.isAccessAllowed( req, Mockito.mock(ServletResponse.class), Mockito.mock(Object.class))) .isFalse(); } finally { ThreadContext.unbindSubject(); ThreadContext.unbindSecurityManager(); } }
@Test public void testAuthorizationValid() throws Exception { try { SecurityUtils.setSecurityManager(new DefaultSecurityManager()); HttpServletRequest req = Mockito.mock(HttpServletRequest.class); Principal principal = Mockito.mock(Principal.class); Mockito.when(principal.getName()).thenReturn("test-user"); Mockito.when(req.getUserPrincipal()).thenReturn(principal); String jwt = new ShiroJwtProvider(Mockito.mock(AppContext.class)).getJwt(req); Mockito.when(req.getHeader("Authorization")).thenReturn("Bearer " + jwt); ShiroJwtVerifyingFilter filter = new ShiroJwtVerifyingFilter(); Assertions.assertThat( filter.isAccessAllowed( req, Mockito.mock(ServletResponse.class), Mockito.mock(Object.class))) .isTrue(); } finally { ThreadContext.unbindSubject(); ThreadContext.unbindSecurityManager(); } }