AdminInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { if (request.getUserPrincipal() == null) { return false; } LOGGER.info( "<preHandle> username: {}, request: {}, query: {}", request.getUserPrincipal().getName(), request.getRequestURI(), request.getQueryString()); return true; }  @Override boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler);  }
@Test public void anyAuthenticatedUserHasAdminRights() { when(requestMock.getUserPrincipal()).thenReturn(() -> random(ThreadLocalRandom.current().nextInt(100))); assertThat(subject.preHandle(requestMock, null, null)).isTrue(); when(requestMock.getUserPrincipal()).thenReturn(null); assertThat(subject.preHandle(requestMock, null, null)).isFalse(); }