Repository navigation
Replies: 3 comments 2 replies
|
Environment:
server.port=8002 # modify by application startup command line
keycloak.auth-server-url=http://172.20.51.25:8080/auth
keycloak.realm=autocv
keycloak.public-client=true
keycloak.resource=oauthtest
keycloak.securityConstraints[0].authRoles[0]=aps
keycloak.securityConstraints[0].securityCollections[0].name= common
keycloak.securityConstraints[0].securityCollections[0].patterns[0]=/* |
0 replies
|
The Keycloak Spring Boot adapter doesn't support Redis I believe, but I would recommend looking at Spring Security as it now has excellent OAuth support on its own without the need for a Keycloak adapter. |
2 replies
|
Store Keycloak tokens in Redis using a custom @Component
public class RedisTokenStore {
private final StringRedisTemplate redis;
private final Duration ttl = Duration.ofMinutes(5); // match access_token lifespan
public RedisTokenStore(StringRedisTemplate redis) {
this.redis = redis;
}
public void store(String tokenId, OAuth2AccessToken token) {
redis.opsForValue().set(
"kc:token:" + tokenId,
serialize(token),
ttl
);
}
public OAuth2AccessToken get(String tokenId) {
String data = redis.opsForValue().get("kc:token:" + tokenId);
return data != null ? deserialize(data) : null;
}
public void remove(String tokenId) {
redis.delete("kc:token:" + tokenId);
}
}For the admin client, cache the access token in Redis to avoid repeated token requests: @Component
public class KeycloakTokenCache {
@Autowired private StringRedisTemplate redis;
public String getAdminToken() {
String cached = redis.opsForValue().get("kc:admin:token");
if (cached != null) return cached;
// Fetch new token
String token = fetchFromKeycloak();
// Cache for 4 minutes (tokens usually last 5 min)
redis.opsForValue().set("kc:admin:token", token, Duration.ofMinutes(4));
return token;
}
private String fetchFromKeycloak() {
// Use KeycloakInstalled or ClientCredentialsGrant
return keycloak.tokenManager().getAccessTokenString();
}
}For a standalone approach (Node.js/Python), use Redis TTL matching the token expiry from Keycloak's JWKS endpoint: import redis, jwt, requests
r = redis.Redis()
def get_token(username, password):
# Try cache first
cached = r.get(f"kc:user:{username}:token")
if cached: return cached.decode()
# Fetch from Keycloak
resp = requests.post(
"http://keycloak:8080/realms/myrealm/protocol/openid-connect/token",
data={"client_id": "myapp", "username": username, "password": password,
"grant_type": "password"}
)
token_data = resp.json()
access_token = token_data["access_token"]
# Decode to get expiry
decoded = jwt.decode(access_token, options={"verify_signature": False})
exp = decoded["exp"] - int(time.time()) - 60 # 1 min buffer
r.setex(f"kc:user:{username}:token", exp, access_token)
return access_token |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Hi Everyone,
I'm trying to use keycloak in my load balance service with Nginx.But when I check the office document, Unfortunately, I only found a config field named
token-storewith two optionssessionandcookiein chapter "Application Clustering".I want to store token in redis so that my application sevices can share it but I don't know how to complete it.Looking forward to your answer, thanks
All reactions