Skip to content

Commit 6a8bba8

Browse files
committed
Add RedisSessionConfig
1 parent 0be94e9 commit 6a8bba8

2 files changed

Lines changed: 104 additions & 14 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* ContainerProxy
3+
*
4+
* Copyright (C) 2016-2021 Open Analytics
5+
*
6+
* ===========================================================================
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the Apache License as published by
10+
* The Apache Software Foundation, either version 2 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* Apache License for more details.
17+
*
18+
* You should have received a copy of the Apache License
19+
* along with this program. If not, see <http://www.apache.org/licenses/>
20+
*/
21+
package eu.openanalytics.containerproxy;
22+
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
26+
import org.springframework.boot.autoconfigure.session.RedisSessionProperties;
27+
import org.springframework.boot.autoconfigure.session.SessionProperties;
28+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
29+
import org.springframework.context.annotation.Bean;
30+
import org.springframework.context.annotation.Configuration;
31+
import org.springframework.core.env.Environment;
32+
import org.springframework.session.data.redis.config.ConfigureNotifyKeyspaceEventsAction;
33+
import org.springframework.session.data.redis.config.ConfigureRedisAction;
34+
import org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration;
35+
36+
import javax.inject.Inject;
37+
import java.time.Duration;
38+
39+
@ConditionalOnProperty(name = "spring.session.store-type", havingValue = "redis")
40+
@Configuration(proxyBeanMethods = false)
41+
@EnableConfigurationProperties(RedisSessionProperties.class)
42+
public class RedisSessionConfig extends RedisHttpSessionConfiguration {
43+
44+
private String redisNamespace;
45+
46+
@Inject
47+
private Environment environment;
48+
49+
@Bean
50+
@ConditionalOnMissingBean
51+
public ConfigureRedisAction configureRedisAction(RedisSessionProperties redisSessionProperties) {
52+
switch (redisSessionProperties.getConfigureAction()) {
53+
case NOTIFY_KEYSPACE_EVENTS:
54+
return new ConfigureNotifyKeyspaceEventsAction();
55+
case NONE:
56+
return ConfigureRedisAction.NO_OP;
57+
}
58+
throw new IllegalStateException(
59+
"Unsupported redis configure action '" + redisSessionProperties.getConfigureAction() + "'.");
60+
61+
}
62+
63+
@Autowired
64+
public void customize(SessionProperties sessionProperties, RedisSessionProperties redisSessionProperties) {
65+
Duration timeout = sessionProperties.getTimeout();
66+
if (timeout != null) {
67+
setMaxInactiveIntervalInSeconds((int) timeout.getSeconds());
68+
}
69+
setFlushMode(redisSessionProperties.getFlushMode());
70+
setSaveMode(redisSessionProperties.getSaveMode());
71+
setCleanupCron(redisSessionProperties.getCleanupCron());
72+
73+
String realmId = environment.getProperty("proxy.realm-id");
74+
75+
if (realmId != null) {
76+
redisNamespace = String.format("shinyproxy__%s__%s", realmId, redisSessionProperties.getNamespace());
77+
} else {
78+
redisNamespace = String.format("shinyproxy__%s", redisSessionProperties.getNamespace());
79+
}
80+
81+
setRedisNamespace(redisNamespace);
82+
}
83+
84+
public String getNamespace() {
85+
return redisNamespace;
86+
}
87+
88+
}

src/main/java/eu/openanalytics/containerproxy/service/session/redis/RedisSessionService.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
*/
2121
package eu.openanalytics.containerproxy.service.session.redis;
2222

23+
import eu.openanalytics.containerproxy.RedisSessionConfig;
2324
import eu.openanalytics.containerproxy.service.session.ISessionService;
2425
import org.apache.commons.logging.Log;
2526
import org.apache.commons.logging.LogFactory;
2627
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
27-
import org.springframework.boot.autoconfigure.session.RedisSessionProperties;
2828
import org.springframework.data.redis.core.RedisTemplate;
29-
import org.springframework.security.core.session.SessionInformation;
30-
import org.springframework.security.core.session.SessionRegistry;
29+
import org.springframework.security.core.context.SecurityContext;
30+
import org.springframework.session.Session;
3131
import org.springframework.session.data.redis.RedisIndexedSessionRepository;
3232
import org.springframework.stereotype.Component;
3333

@@ -54,10 +54,7 @@ public class RedisSessionService implements ISessionService {
5454
private RedisIndexedSessionRepository redisIndexedSessionRepository;
5555

5656
@Inject
57-
private SessionRegistry sessionRegistry;
58-
59-
@Inject
60-
private RedisSessionProperties redisSessionProperties;
57+
private RedisSessionConfig redisSessionConfig;
6158

6259
private String keyPattern;
6360
private RedisTemplate<Object, Object> redisTemplate;
@@ -66,12 +63,12 @@ public class RedisSessionService implements ISessionService {
6663

6764
@PostConstruct
6865
public void init() {
69-
keyPattern = redisSessionProperties.getNamespace() + ":sessions:*";
66+
keyPattern = redisSessionConfig.getNamespace() + ":sessions:*";
7067
redisTemplate = (RedisTemplate<Object, Object>) redisIndexedSessionRepository.getSessionRedisOperations();
7168
new Timer().schedule(new TimerTask() {
7269
@Override
7370
public void run() {
74-
updateCachedUsersLoggedInCount();
71+
updateCachedUsersLoggedInCount();
7572
}
7673
}, 0, CACHE_UPDATE_INTERVAL);
7774
}
@@ -96,16 +93,21 @@ private void updateCachedUsersLoggedInCount() {
9693
return;
9794
}
9895

99-
System.out.println(keys);
100-
10196
Set<String> authenticatedUsers = keys
10297
.stream()
10398
.map((keyId) -> {
10499
String sessionId = extractSessionId(keyId);
105100
if (sessionId != null) {
106-
SessionInformation sessionInformation = sessionRegistry.getSessionInformation(sessionId);
107-
if (sessionInformation != null) {
108-
return sessionInformation.getPrincipal().toString();
101+
logger.debug(String.format("Extracted sessionId %s ", sessionId));
102+
Session session = redisIndexedSessionRepository.findById(sessionId);
103+
if (session != null) {
104+
Object object = session.getAttribute("SPRING_SECURITY_CONTEXT");
105+
if (object instanceof SecurityContext) {
106+
SecurityContext securityContext = (SecurityContext) object;
107+
if (securityContext.getAuthentication().isAuthenticated()) {
108+
return securityContext.getAuthentication().getName();
109+
}
110+
}
109111
}
110112
}
111113
return null;

0 commit comments

Comments
 (0)