-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new 'uPortal-session' submodule which adds support for sess…
…ion clustering/replication/failover with Redis
- Loading branch information
Showing
14 changed files
with
196 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
description = "Apereo uPortal Session" | ||
|
||
dependencies { | ||
compile "org.springframework:spring-web:${springVersion}" | ||
compile "org.springframework.session:spring-session-data-redis:${springSessionVersion}" | ||
compileOnly "${servletApiDependency}" | ||
} |
10 changes: 10 additions & 0 deletions
10
uPortal-session/src/main/java/org/apereo/portal/session/PortalSessionConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.apereo.portal.session; | ||
|
||
public class PortalSessionConstants { | ||
|
||
private PortalSessionConstants() {} | ||
|
||
public static final String REDIS_STORE_TYPE = "redis"; | ||
public static final String SESSION_STORE_TYPE_ENV_PROPERTY_NAME = "SPRING_SESSION_STORETYPE"; | ||
public static final String SESSION_STORE_TYPE_SYSTEM_PROPERTY_NAME = "spring.session.storetype"; | ||
} |
24 changes: 24 additions & 0 deletions
24
uPortal-session/src/main/java/org/apereo/portal/session/redis/RedisSessionConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.apereo.portal.session.redis; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Conditional; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; | ||
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; | ||
|
||
@Configuration | ||
@Conditional(SpringSessionRedisEnabledCondition.class) | ||
@EnableRedisHttpSession | ||
public class RedisSessionConfig { | ||
|
||
// @Bean | ||
// @Primary | ||
// public RedisProperties redisProperties() { | ||
// return new RedisProperties(); | ||
// } | ||
|
||
@Bean | ||
public JedisConnectionFactory redisConnectionFactory() { | ||
return new JedisConnectionFactory(); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
uPortal-session/src/main/java/org/apereo/portal/session/redis/RedisSessionInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.apereo.portal.session.redis; | ||
|
||
import static org.apereo.portal.session.PortalSessionConstants.REDIS_STORE_TYPE; | ||
import static org.apereo.portal.session.PortalSessionConstants.SESSION_STORE_TYPE_ENV_PROPERTY_NAME; | ||
import static org.apereo.portal.session.PortalSessionConstants.SESSION_STORE_TYPE_SYSTEM_PROPERTY_NAME; | ||
|
||
import org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer; | ||
|
||
/** | ||
* This class is needed to enable Spring Session Redis support in uPortal. It registers the filter | ||
* that is needed by Spring Session to manage the session with Redis. It also ensures that the | ||
* filter is only registered if the session store-type is configured for Redis. The filter could | ||
* have instead been added to web.xml, but that would not have allowed for the feature to be | ||
* enabled/disabled via configuration. Note that the application properties are not available during | ||
* initialization, and therefore we instead check for an environment variable or system property. | ||
*/ | ||
public class RedisSessionInitializer extends AbstractHttpSessionApplicationInitializer { | ||
|
||
public RedisSessionInitializer() { | ||
// MUST pass null here to avoid having Spring Session create a root WebApplicationContext | ||
// that does not work | ||
// with the current uPortal setup. | ||
super((Class<?>[]) null); | ||
} | ||
|
||
@Override | ||
public void onStartup(javax.servlet.ServletContext servletContext) | ||
throws javax.servlet.ServletException { | ||
if (REDIS_STORE_TYPE.equals(this.getStoreTypeConfiguredValue())) { | ||
super.onStartup(servletContext); | ||
} | ||
} | ||
|
||
private String getStoreTypeConfiguredValue() { | ||
String result = System.getProperty(SESSION_STORE_TYPE_SYSTEM_PROPERTY_NAME); | ||
if (result == null) { | ||
result = System.getenv(SESSION_STORE_TYPE_ENV_PROPERTY_NAME); | ||
} | ||
return result; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...ion/src/main/java/org/apereo/portal/session/redis/SpringSessionRedisEnabledCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.apereo.portal.session.redis; | ||
|
||
import static org.apereo.portal.session.PortalSessionConstants.REDIS_STORE_TYPE; | ||
import static org.apereo.portal.session.PortalSessionConstants.SESSION_STORE_TYPE_ENV_PROPERTY_NAME; | ||
import static org.apereo.portal.session.PortalSessionConstants.SESSION_STORE_TYPE_SYSTEM_PROPERTY_NAME; | ||
|
||
import org.springframework.context.annotation.Condition; | ||
import org.springframework.context.annotation.ConditionContext; | ||
import org.springframework.core.type.AnnotatedTypeMetadata; | ||
|
||
public class SpringSessionRedisEnabledCondition implements Condition { | ||
|
||
@Override | ||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { | ||
return REDIS_STORE_TYPE.equals(this.getSessionStoreTypeValue(context)); | ||
} | ||
|
||
private String getSessionStoreTypeValue(ConditionContext context) { | ||
String result = | ||
context.getEnvironment() | ||
.getProperty(SESSION_STORE_TYPE_SYSTEM_PROPERTY_NAME, String.class, null); | ||
if (result == null) { | ||
result = | ||
context.getEnvironment() | ||
.getProperty(SESSION_STORE_TYPE_ENV_PROPERTY_NAME, String.class, null); | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
uPortal-webapp/src/main/java/org/apereo/portal/PortalWebAppInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.apereo.portal; | ||
|
||
import org.springframework.web.context.AbstractContextLoaderInitializer; | ||
import org.springframework.web.context.WebApplicationContext; | ||
import org.springframework.web.context.support.XmlWebApplicationContext; | ||
|
||
/** | ||
* This class does the following: 1. creates the root application context using the specified config | ||
* locations 2. initializes the context loader | ||
* | ||
* <p>This replaces the the following, which were previously defined in web.xml file. | ||
* | ||
* <pre>{@code | ||
* <context-param> | ||
* <param-name>contextConfigLocation</param-name> | ||
* <param-value>classpath:/properties/contexts/*.xml,classpath:/properties/contextOverrides/*.xml</param-value> | ||
* </context-param> | ||
* | ||
* <!-- | ||
* | Loads/Unloads the Spring WebApplicationContext | ||
* +--> | ||
* <listener> | ||
* <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> | ||
* </listener> | ||
* | ||
* }</pre> | ||
* | ||
* This new approach allows us to dynamically update the servlet context programatically with | ||
* Spring, which was needed in order support Spring Session handling as a feature that could be | ||
* enabled/disabled with configuration. | ||
*/ | ||
public class PortalWebAppInitializer extends AbstractContextLoaderInitializer { | ||
|
||
@Override | ||
protected WebApplicationContext createRootApplicationContext() { | ||
XmlWebApplicationContext context = new XmlWebApplicationContext(); | ||
context.setConfigLocation( | ||
"classpath:/properties/contexts/*.xml,classpath:/properties/contextOverrides/*.xml"); | ||
return context; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
uPortal-webapp/src/main/resources/properties/contexts/sessionContext.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Licensed to Apereo under one or more contributor license | ||
agreements. See the NOTICE file distributed with this work | ||
for additional information regarding copyright ownership. | ||
Apereo licenses this file to you under the Apache License, | ||
Version 2.0 (the "License"); you may not use this file | ||
except in compliance with the License. You may obtain a | ||
copy of the License at the following location: | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, | ||
software distributed under the License is distributed on an | ||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations | ||
under the License. | ||
--> | ||
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://www.springframework.org/schema/beans" | ||
xmlns:context="http://www.springframework.org/schema/context" | ||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd | ||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> | ||
|
||
<context:annotation-config/> | ||
<context:component-scan base-package="org.apereo.portal.session"/> | ||
|
||
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters