@Test void givenServiceIdAndJwt_whenExpiringCommand_thenReturnNewOne() { AbstractAuthenticationScheme scheme = mock(AbstractAuthenticationScheme.class); Application application = createApplication( createInstanceInfo("instanceId", AuthenticationScheme.HTTP_BASIC_PASSTICKET, "applid") ); doReturn(application).when(discoveryClient).getApplication("serviceId"); doReturn(scheme).when(authenticationSchemeFactory).getSchema(any()); AuthenticationCommandTest cmd = new AuthenticationCommandTest(false); doReturn(cmd).when(scheme).createCommand(any(), any()); assertSame(cmd, serviceAuthenticationService.getAuthenticationCommand("serviceId", "jwt")); verify(scheme, times(1)).createCommand(any(), any()); assertSame(cmd, serviceAuthenticationService.getAuthenticationCommand("serviceId", "jwt")); verify(scheme, times(1)).createCommand(any(), any()); cmd.setExpired(true); AuthenticationCommand cmd2 = new AuthenticationCommandTest(false); reset(scheme); doReturn(cmd2).when(scheme).createCommand(any(), any()); assertSame(cmd2, serviceAuthenticationService.getAuthenticationCommand("serviceId", "jwt")); verify(scheme, times(1)).createCommand(any(), any()); assertSame(cmd2, serviceAuthenticationService.getAuthenticationCommand("serviceId", "jwt")); verify(scheme, times(1)).createCommand(any(), any()); }
@Override @CacheEvict(value = CACHE_BY_AUTHENTICATION, condition = "#result != null && #result.isExpired()") @Cacheable(CACHE_BY_AUTHENTICATION) public AuthenticationCommand getAuthenticationCommand(Authentication authentication, String jwtToken) { final AbstractAuthenticationScheme scheme = authenticationSchemeFactory.getSchema(authentication.getScheme()); if (jwtToken == null) return scheme.createCommand(authentication, () -> null); return scheme.createCommand(authentication, () -> authenticationService.parseJwtToken(jwtToken)); }
ServiceAuthenticationServiceImpl implements ServiceAuthenticationService { @Override @CacheEvict(value = CACHE_BY_AUTHENTICATION, condition = "#result != null && #result.isExpired()") @Cacheable(CACHE_BY_AUTHENTICATION) public AuthenticationCommand getAuthenticationCommand(Authentication authentication, String jwtToken) { final AbstractAuthenticationScheme scheme = authenticationSchemeFactory.getSchema(authentication.getScheme()); if (jwtToken == null) return scheme.createCommand(authentication, () -> null); return scheme.createCommand(authentication, () -> authenticationService.parseJwtToken(jwtToken)); } }
ServiceAuthenticationServiceImpl implements ServiceAuthenticationService { @Override @CacheEvict(value = CACHE_BY_AUTHENTICATION, condition = "#result != null && #result.isExpired()") @Cacheable(CACHE_BY_AUTHENTICATION) public AuthenticationCommand getAuthenticationCommand(Authentication authentication, String jwtToken) { final AbstractAuthenticationScheme scheme = authenticationSchemeFactory.getSchema(authentication.getScheme()); if (jwtToken == null) return scheme.createCommand(authentication, () -> null); return scheme.createCommand(authentication, () -> authenticationService.parseJwtToken(jwtToken)); }  }
ServiceAuthenticationServiceImpl implements ServiceAuthenticationService { @Override @CacheEvict(value = CACHE_BY_AUTHENTICATION, condition = "#result != null && #result.isExpired()") @Cacheable(CACHE_BY_AUTHENTICATION) public AuthenticationCommand getAuthenticationCommand(Authentication authentication, String jwtToken) { final AbstractAuthenticationScheme scheme = authenticationSchemeFactory.getSchema(authentication.getScheme()); if (jwtToken == null) return scheme.createCommand(authentication, () -> null); return scheme.createCommand(authentication, () -> authenticationService.parseJwtToken(jwtToken)); }  Authentication getAuthentication(InstanceInfo instanceInfo); @Override @CacheEvict(value = CACHE_BY_AUTHENTICATION, condition = "#result != null && #result.isExpired()") @Cacheable(CACHE_BY_AUTHENTICATION) AuthenticationCommand getAuthenticationCommand(Authentication authentication, String jwtToken); @Override @RetryIfExpired @CacheEvict( value = CACHE_BY_SERVICE_ID, condition = "#result != null && #result.isExpired()", keyGenerator = CacheConfig.COMPOSITE_KEY_GENERATOR ) @Cacheable(value = CACHE_BY_SERVICE_ID, keyGenerator = CacheConfig.COMPOSITE_KEY_GENERATOR) AuthenticationCommand getAuthenticationCommand(String serviceId, String jwtToken); @Override @CacheEvict(value = CACHE_BY_SERVICE_ID, allEntries = true) void evictCacheAllService(); @Override void evictCacheService(String serviceId); }
ServiceAuthenticationServiceImpl implements ServiceAuthenticationService { @Override @CacheEvict(value = CACHE_BY_AUTHENTICATION, condition = "#result != null && #result.isExpired()") @Cacheable(CACHE_BY_AUTHENTICATION) public AuthenticationCommand getAuthenticationCommand(Authentication authentication, String jwtToken) { final AbstractAuthenticationScheme scheme = authenticationSchemeFactory.getSchema(authentication.getScheme()); if (jwtToken == null) return scheme.createCommand(authentication, () -> null); return scheme.createCommand(authentication, () -> authenticationService.parseJwtToken(jwtToken)); }  Authentication getAuthentication(InstanceInfo instanceInfo); @Override @CacheEvict(value = CACHE_BY_AUTHENTICATION, condition = "#result != null && #result.isExpired()") @Cacheable(CACHE_BY_AUTHENTICATION) AuthenticationCommand getAuthenticationCommand(Authentication authentication, String jwtToken); @Override @RetryIfExpired @CacheEvict( value = CACHE_BY_SERVICE_ID, condition = "#result != null && #result.isExpired()", keyGenerator = CacheConfig.COMPOSITE_KEY_GENERATOR ) @Cacheable(value = CACHE_BY_SERVICE_ID, keyGenerator = CacheConfig.COMPOSITE_KEY_GENERATOR) AuthenticationCommand getAuthenticationCommand(String serviceId, String jwtToken); @Override @CacheEvict(value = CACHE_BY_SERVICE_ID, allEntries = true) void evictCacheAllService(); @Override void evictCacheService(String serviceId); static final String AUTHENTICATION_COMMAND_KEY; }