title: Spring-boot Resource server validate token only when expired
body: <p>I`m developing a microservice structure using spring-boot, this structure has an external oauth2 Authorization Server and multiples Resource Servers.</p>
  <p>My problem is, each http request to my resources, calls a url to my Authorization Server, to validate the token ( <code>.../oauth/check_token/</code>). This way there is a lot of requests.  There is a way to validate/check this token only when it is expired?</p>
  <p>My Resource servers:</p>
  <pre><code>@Configuration
  @EnableResourceServer
  public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
      
      @Value(&quot;${security.oauth2.client.client-id}&quot;)
      private String clientId;
      
      @Value(&quot;${security.oauth2.client.client-secret}&quot;)
      private String clientSecret;
      
      @Value(&quot;${security.oauth2.resource.id}&quot;)
      private String resourceId;
      
      @Value(&quot;${security.oauth2.resource.token-info-uri}&quot;)
      private String tokenInfoUri;
  
      @Override
      public void configure(HttpSecurity http) throws Exception {
          http
              .authorizeRequests()
              .antMatchers(ADMIN_ANT_MATCHER).hasRole(&quot;ADMIN&quot;)
              .antMatchers(PROTECTED_ANT_MATCHER).hasRole(&quot;USER&quot;)
              .and()
              .csrf().disable();
      }
      
      @Override
      public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
           resources.tokenServices(tokenService()).resourceId(resourceId).stateless(true);
      }
      
      @Bean
      @Primary
      public RemoteTokenServices tokenService() {
          RemoteTokenServices tokenService = new RemoteTokenServices();
          tokenService.setCheckTokenEndpointUrl(tokenInfoUri);
          tokenService.setClientId(clientId);
          tokenService.setClientSecret(clientSecret);
          return tokenService;
      }
  }
  </code></pre>
  <p>Authorization Server:</p>
  <pre><code>@Configuration
  @EnableAuthorizationServer
  public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
  
      @Value(&quot;${security.oauth2.client.client-id}&quot;)
      private String clientId;
  
      @Value(&quot;${security.oauth2.client.client-secret}&quot;)
      private String clientSecret;
  
      @Value(&quot;${security.oauth2.resource.id}&quot;)
      private String resourceId;
  
      @Value(&quot;${security.oauth2.client.access-token-validity-seconds}&quot;)
      private Integer tokenValidateSeconds;
  
      @Value(&quot;${security.oauth2.client.token-secret}&quot;)
      private String tokenSecret;
  
      @Autowired
      private AuthenticationManager authenticationManager;
  
      @Autowired
      private TokenStore tokenStore;
  
      @Autowired
      private UserDetailsService userDetailsService;
  
      @Autowired
      private OauthAccessTokenRepository oauthAccessTokenRepository;
  
      @Autowired
      private OauthRefreshTokenRepository oauthRefreshTokenRepository;
  
  
      @Override
      public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
          endpoints
                  .tokenStore(this.tokenStore)
                  .tokenEnhancer(tokenEnhancer())
                  .authenticationManager(this.authenticationManager)
                  .userDetailsService(userDetailsService);
      }
  
      @Override
      public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
          clients
                  .inMemory()
                  .withClient(clientId)
                  .authorizedGrantTypes(&quot;client_credentials&quot;, &quot;password&quot;, &quot;refresh_token&quot;)
                  .authorities(&quot;ROLE_USER&quot;,&quot;ROLE_ADMIN&quot;)
                  .scopes(&quot;read&quot;,&quot;write&quot;,&quot;trust&quot;)
                  .resourceIds(resourceId)
                  .accessTokenValiditySeconds(tokenValidateSeconds)
                  .secret(bCryptPasswordEncoder().encode(clientSecret));
      }
  
      @Override
      public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
          security.checkTokenAccess(&quot;isAuthenticated()&quot;);
      }
  
      @Bean
      public BCryptPasswordEncoder bCryptPasswordEncoder(){
          return new BCryptPasswordEncoder();
      }
  
      @Bean
      @Primary
      public DefaultTokenServices tokenServices() {
          DefaultTokenServices tokenServices = new DefaultTokenServices();
          tokenServices.setSupportRefreshToken(true);
          tokenServices.setTokenStore(this.tokenStore);
          return tokenServices;
      }
  
      @Bean
      public TokenEnhancer tokenEnhancer() {
          return new TokenEnhancer() {
              @Override
              public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
                  CustomUser user = (CustomUser) authentication.getPrincipal();
  
                  String token = JwtTokenHelper.generateToken(
                          user.getIdUser(),
                          user.getTenantID(),
                          user.getIdEntity(),
                          user.getIdBusinessUnit(),
                          user.getProfile(),
                          tokenValidateSeconds,
                          tokenSecret
                  );
  
                  ((DefaultOAuth2AccessToken) accessToken).setValue(token);
  
                  return accessToken;
              }
          };
  
      }
  }
  </code></pre>