RED-6686 - token based web request interceptor

This commit is contained in:
Timo Bejan 2023-06-27 23:04:23 +03:00
parent 814dc15efb
commit 476b78528c
3 changed files with 8 additions and 5 deletions

View File

@ -10,7 +10,7 @@
</parent>
<groupId>com.knecon.fforesight</groupId>
<artifactId>keycloak-commons</artifactId>
<version>0.1-SNAPSHOT</version>
<version>0.2-SNAPSHOT</version>
<name>keycloak-commons</name>
<description>keycloak-commons</description>
<properties>

View File

@ -2,6 +2,7 @@ package com.knecon.fforesight.keycloakcommons;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@ -21,7 +22,7 @@ public class MultiTenancyJwtBasedWebConfiguration implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addWebRequestInterceptor(jwtBasedTenantInterceptor);
registry.addWebRequestInterceptor(jwtBasedTenantInterceptor).order(Ordered.LOWEST_PRECEDENCE);
}
}

View File

@ -6,15 +6,17 @@ import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
import org.springframework.stereotype.Service;
import com.knecon.fforesight.keycloakcommons.security.TokenUtils;
@Service
public class TokenRealmService {
public Optional<String> getRealm() {
var authentication = (JwtAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
return Optional.of(TokenUtils.toTenant(authentication.getToken().getTokenValue()));
if (authentication != null && authentication.getToken() != null && authentication.getToken().getTokenValue() != null) {
return Optional.of(TokenUtils.toTenant(authentication.getToken().getTokenValue()));
} else {
return Optional.empty();
}
}
}