@Test public void doFilter() throws IOException, ServletException { when(authentication.getName()).thenReturn("user"); doReturn(Sets.newHashSet( new SimpleGrantedAuthority("authority1"), new SimpleGrantedAuthority("authority2"))) .when(authentication).getAuthorities(); when(session.getAttribute("authenticationHashCode")).thenReturn(123); when(request.getSession()).thenReturn(session); SecurityContextImpl securityContext = new SecurityContextImpl(); securityContext.setAuthentication(authentication); SecurityContextHolder.setContext(securityContext); new AuthenticationCreationTimeFilter().doFilter(request, response, filterChain); SecurityContextHolder.clearContext(); ArgumentCaptor<Long> timeArgument = ArgumentCaptor.forClass(Long.class); verify(session).setAttribute(eq("authenticationCreationTime"), timeArgument.capture()); TestUtil.assertSecondsAgo(new Date(timeArgument.getValue()), 5); }
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); int hashCode = getHashCode(authentication); HttpSession session = ((HttpServletRequest) request).getSession(); Integer lastAuthenticationHashCode = (Integer) session.getAttribute(AUTHENTICATION_HASH_CODE); if ((lastAuthenticationHashCode == null) || (lastAuthenticationHashCode != hashCode)) { session.setAttribute(AUTHENTICATION_HASH_CODE, hashCode); AuthenticationUtil.setAuthenticationCreationTime(session, System.currentTimeMillis()); } chain.doFilter(request, response); }
AuthenticationCreationTimeFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); int hashCode = getHashCode(authentication); HttpSession session = ((HttpServletRequest) request).getSession(); Integer lastAuthenticationHashCode = (Integer) session.getAttribute(AUTHENTICATION_HASH_CODE); if ((lastAuthenticationHashCode == null) || (lastAuthenticationHashCode != hashCode)) { session.setAttribute(AUTHENTICATION_HASH_CODE, hashCode); AuthenticationUtil.setAuthenticationCreationTime(session, System.currentTimeMillis()); } chain.doFilter(request, response); } }
AuthenticationCreationTimeFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); int hashCode = getHashCode(authentication); HttpSession session = ((HttpServletRequest) request).getSession(); Integer lastAuthenticationHashCode = (Integer) session.getAttribute(AUTHENTICATION_HASH_CODE); if ((lastAuthenticationHashCode == null) || (lastAuthenticationHashCode != hashCode)) { session.setAttribute(AUTHENTICATION_HASH_CODE, hashCode); AuthenticationUtil.setAuthenticationCreationTime(session, System.currentTimeMillis()); } chain.doFilter(request, response); }  }
AuthenticationCreationTimeFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); int hashCode = getHashCode(authentication); HttpSession session = ((HttpServletRequest) request).getSession(); Integer lastAuthenticationHashCode = (Integer) session.getAttribute(AUTHENTICATION_HASH_CODE); if ((lastAuthenticationHashCode == null) || (lastAuthenticationHashCode != hashCode)) { session.setAttribute(AUTHENTICATION_HASH_CODE, hashCode); AuthenticationUtil.setAuthenticationCreationTime(session, System.currentTimeMillis()); } chain.doFilter(request, response); }  @Override void init(FilterConfig filterConfig); @Override void destroy(); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); }
AuthenticationCreationTimeFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); int hashCode = getHashCode(authentication); HttpSession session = ((HttpServletRequest) request).getSession(); Integer lastAuthenticationHashCode = (Integer) session.getAttribute(AUTHENTICATION_HASH_CODE); if ((lastAuthenticationHashCode == null) || (lastAuthenticationHashCode != hashCode)) { session.setAttribute(AUTHENTICATION_HASH_CODE, hashCode); AuthenticationUtil.setAuthenticationCreationTime(session, System.currentTimeMillis()); } chain.doFilter(request, response); }  @Override void init(FilterConfig filterConfig); @Override void destroy(); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain);  }