Spring Keycloak Adapter sometimes returns with "Token is not active" when uploading files

971 Views Asked by At

lately we are facing an issue that our Spring Boot backend service (stateless REST service) SOMETIMES returns an HTTP 401 (Unauthorized) error when users try to upload files >70 MB (or in other words, when the request takes longer than just a couple of seconds). This does not occur consistently and only happens sometimes (~every second or third attempt).

The www-authenticate header contains the following in these cases: Bearer realm="test", error "invalid_token", error_description="Token is not active"

Our Spring (Boot) configuration is simple:

keycloak.auth-server-url=${KEYCLOAK_URL:http://keycloak:8080/auth}
keycloak.realm=${KEYCLOAK_REALM:test}
keycloak.resource=${KEYCLOAK_CLIENT:test}
keycloak.cors=true
keycloak.bearer-only=true

Essentially, our frontend code uses keycloak-js and does the following to keep the access token fresh:

setInterval(() => {
 // updates the token if it expires within the next 5s
 this.keycloak.updateToken(5).then((refreshed) => {
   console.log('Access token updated:', refreshed)
   if (refreshed) {
     store.commit(AuthMutationTypes.SET_TOKEN, this.keycloak.token);
   }
 }).catch(() => {
   console.log('Failed to refresh token');
 });
}, 300);

Further, we use Axios and a respective request filter to inject the current token:

axios.interceptors.request.use(
  (request: AxiosRequestConfig) => {
    if (store.getters.isAuthenticated) {
      request.headers.Authorization = 'Bearer ' + store.getters.token;
    }
    return request;
  }
);

This worked very well so far and we have never experienced such a thing for our usual GETs/POSTs/PUTs etc. This happens only when users try to upload files larger than (around) 70MBish.

Any hint or tip how to debug this any further? We appreciate any help...

Cheers

0

There are 0 best solutions below