Skip to content

Commit 39df088

Browse files
committed
changes to support single-sign-on (sso)
1 parent 372332e commit 39df088

File tree

4 files changed

+44
-21
lines changed

4 files changed

+44
-21
lines changed

src/main/java/tomcat/request/session/model/Config.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
/** author: Ranjith Manickam @ 5 Feb' 2020 */
1212
public class Config implements Serializable {
1313

14+
private static final long serialVersionUID = 3480402257971437776L;
15+
1416
public static final String APPLICATION_PROPERTIES_FILE = "redis-data-cache.properties";
1517

1618
/** Redis config type. */
@@ -77,6 +79,9 @@ public enum RedisConfigType {
7779
@Property(name = "session.persistent.policies", defaultValue = "DEFAULT")
7880
private String sessionPersistentPolicies;
7981

82+
@Property(name = "redis.sso.timeout", type = INTEGER, defaultValue = "0")
83+
private Integer redisSSOTimeout;
84+
8085
public Config() {
8186
}
8287

@@ -98,7 +103,8 @@ public Config(String redisHosts,
98103
String redisSentinelMaster,
99104
Integer redisSessionExpiryJobInterval,
100105
Integer redisSessionDataSyncJobInterval,
101-
String sessionPersistentPolicies) {
106+
String sessionPersistentPolicies,
107+
Integer redisSSOTimeout) {
102108
this.redisHosts = redisHosts;
103109
this.redisClusterEnabled = redisClusterEnabled;
104110
this.redisSentinelEnabled = redisSentinelEnabled;
@@ -118,6 +124,7 @@ public Config(String redisHosts,
118124
this.redisSessionExpiryJobInterval = redisSessionExpiryJobInterval;
119125
this.redisSessionDataSyncJobInterval = redisSessionDataSyncJobInterval;
120126
this.sessionPersistentPolicies = sessionPersistentPolicies;
127+
this.redisSSOTimeout = redisSSOTimeout;
121128
}
122129

123130
/** To get 'redis.hosts' value. */
@@ -215,6 +222,11 @@ public String getSessionPersistentPolicies() {
215222
return sessionPersistentPolicies;
216223
}
217224

225+
/** To get 'redis.sso.timeout' value */
226+
public Integer getRedisSSOTimeout() {
227+
return redisSSOTimeout;
228+
}
229+
218230
/** {@inheritDoc} */
219231
@Override
220232
public String toString() {
@@ -238,6 +250,7 @@ public String toString() {
238250
", redisSessionExpiryJobInterval=" + redisSessionExpiryJobInterval +
239251
", redisSessionDataSyncJobInterval=" + redisSessionDataSyncJobInterval +
240252
", sessionPersistentPolicies='" + sessionPersistentPolicies + '\'' +
253+
", redisSSOTimeout='" + redisSSOTimeout + '\'' +
241254
'}';
242255
}
243256

src/main/java/tomcat/request/session/redis/SessionManager.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ public class SessionManager extends ManagerBase implements Lifecycle {
3232

3333
private static final Logger LOGGER = LoggerFactory.getLogger(SessionManager.class);
3434

35+
private Integer ssoTimeout;
3536
private DataCache dataCache;
3637
private SerializationUtil serializer;
37-
private ThreadLocal<SessionContext> sessionContext = new ThreadLocal<>();
38-
private Set<SessionPolicy> sessionPolicy = EnumSet.of(SessionPolicy.DEFAULT);
38+
private final ThreadLocal<SessionContext> sessionContext = new ThreadLocal<>();
39+
private final Set<SessionPolicy> sessionPolicy = EnumSet.of(SessionPolicy.DEFAULT);
3940

4041
public boolean getSaveOnChange() {
4142
return this.sessionPolicy.contains(SessionPolicy.SAVE_ON_CHANGE);
@@ -78,14 +79,6 @@ protected synchronized void startInternal() throws LifecycleException {
7879
initializedValve = true;
7980
break;
8081
}
81-
82-
if (valve instanceof SingleSignOnValve) {
83-
SingleSignOnValve ssoValve = (SingleSignOnValve) valve;
84-
ssoValve.setSessionManager(this);
85-
ssoValve.setContext(context);
86-
initializedValve = true;
87-
break;
88-
}
8982
}
9083

9184
if (!initializedValve) {
@@ -218,6 +211,7 @@ public void unload() {
218211
private void initialize() {
219212
try {
220213
Config config = ConfigUtil.getConfig();
214+
this.ssoTimeout = config.getRedisSSOTimeout();
221215
this.dataCache = new DataCacheFactory(config, getSessionTimeout(null)).getDataCache();
222216
this.serializer = new SerializationUtil();
223217

@@ -353,6 +347,9 @@ void setSingleSignOnEntry(String ssoId, SingleSignOnEntry entry) {
353347
try {
354348
byte[] data = this.serializer.serializeSingleSignOnEntry(entry);
355349
this.dataCache.set(ssoId, data);
350+
if (this.ssoTimeout > 0) {
351+
this.dataCache.expire(ssoId, this.ssoTimeout);
352+
}
356353
} catch (IOException ex) {
357354
LOGGER.error("Error occurred while serializing the single-sign-on entry..", ex);
358355
}

src/main/java/tomcat/request/session/redis/SingleSignOnValve.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import org.apache.catalina.Container;
44
import org.apache.catalina.Context;
5+
import org.apache.catalina.Engine;
6+
import org.apache.catalina.LifecycleException;
57
import org.apache.catalina.Manager;
68
import org.apache.catalina.Realm;
79
import org.apache.catalina.Session;
@@ -26,14 +28,27 @@ public class SingleSignOnValve extends SingleSignOn {
2628

2729
private static final Logger LOGGER = LoggerFactory.getLogger(SingleSignOnValve.class);
2830

29-
private Context context;
31+
private Engine engine;
3032
private SessionManager manager;
3133

34+
/** {@inheritDoc} */
35+
@Override
36+
protected synchronized void startInternal() throws LifecycleException {
37+
Container c;
38+
for (c = this.getContainer(); c != null && !(c instanceof Engine); c = c.getParent()) {
39+
}
40+
41+
if (c instanceof Engine) {
42+
this.engine = (Engine) c;
43+
}
44+
45+
super.startInternal();
46+
}
47+
3248
/** {@inheritDoc} */
3349
@Override
3450
public void invoke(Request request, Response response) throws BackendException {
3551
try {
36-
this.setContext(request.getContext());
3752
this.setSessionManager(request.getContext().getManager());
3853

3954
request.removeNote("org.apache.catalina.request.SSOID");
@@ -213,17 +228,12 @@ void setSessionManager(Manager manager) {
213228
this.manager = (SessionManager) manager;
214229
}
215230

216-
/** To set context. */
217-
void setContext(Context context) {
218-
this.context = context;
219-
}
220-
221231
/** To expire session. */
222232
private void expire(SingleSignOnSessionKey key) {
223-
if (this.context == null) {
233+
if (this.engine == null) {
224234
LOGGER.warn("singleSignOn.sessionExpire.engineNull, key: {}", key);
225235
} else {
226-
Container host = this.context.findChild(key.getHostName());
236+
Container host = this.engine.findChild(key.getHostName());
227237
if (host == null) {
228238
LOGGER.warn("singleSignOn.sessionExpire.hostNotFound, key: {}", key);
229239
} else {

src/main/resources/redis-data-cache.properties

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,7 @@ lb.sticky-session.enabled=false
3333
# policies - DEFAULT, SAVE_ON_CHANGE, ALWAYS_SAVE_AFTER_REQUEST
3434
# 1. SAVE_ON_CHANGE: every time session.setAttribute() or session.removeAttribute() is called the session will be saved.
3535
# 2. ALWAYS_SAVE_AFTER_REQUEST: force saving after every request, regardless of whether or not the manager has detected changes to the session.
36-
session.persistent.policies=DEFAULT
36+
session.persistent.policies=DEFAULT
37+
38+
#- single-sign-on session timeout. (default value: 0 ms (-no expiry))
39+
redis.sso.timeout=0

0 commit comments

Comments
 (0)