|
| 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 | +} |
0 commit comments