From 61cae4b220b5ce81d5194199087a5a3813680002 Mon Sep 17 00:00:00 2001 From: DonaldChung-HK <37064034+DonaldChung-HK@users.noreply.github.com> Date: Fri, 13 Sep 2024 17:32:53 +0000 Subject: [PATCH] add ways to run external session store with JDBC and store session data in separate db --- iam-login-service/pom.xml | 9 ++- .../infn/mw/iam/config/DataSourceConfig.java | 65 +++++++++++++++++++ .../resources/application-jdbc-session.yml | 26 ++++++++ 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 iam-login-service/src/main/java/it/infn/mw/iam/config/DataSourceConfig.java create mode 100644 iam-login-service/src/main/resources/application-jdbc-session.yml diff --git a/iam-login-service/pom.xml b/iam-login-service/pom.xml index 37f7043c2..648c92963 100644 --- a/iam-login-service/pom.xml +++ b/iam-login-service/pom.xml @@ -87,7 +87,14 @@ org.springframework.session spring-session-data-redis - + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.session + spring-session-jdbc + org.springframework.boot spring-boot-starter-data-redis diff --git a/iam-login-service/src/main/java/it/infn/mw/iam/config/DataSourceConfig.java b/iam-login-service/src/main/java/it/infn/mw/iam/config/DataSourceConfig.java new file mode 100644 index 000000000..b78857963 --- /dev/null +++ b/iam-login-service/src/main/java/it/infn/mw/iam/config/DataSourceConfig.java @@ -0,0 +1,65 @@ +/** + * Copyright (c) Istituto Nazionale di Fisica Nucleare (INFN). 2016-2021 + * + * Licensed 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 + * + * 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. + */ +package it.infn.mw.iam.config; + +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; +import org.springframework.session.jdbc.config.annotation.SpringSessionDataSource; + +import com.zaxxer.hikari.HikariDataSource; + +@Configuration +public class DataSourceConfig { + @Bean + @Primary + @ConfigurationProperties("spring.datasource") + @Qualifier("dataSource") + public DataSourceProperties dataSourceProperties() { + return new DataSourceProperties(); + } + + @Bean + @Primary + public HikariDataSource primaryDataSource(@Qualifier("dataSource") DataSourceProperties dataSourceProperties) { + return dataSourceProperties + .initializeDataSourceBuilder() + .type(HikariDataSource.class) + .build(); + } + + @Bean + @ConfigurationProperties("session.datasource") + @Qualifier("sessionDataSourceProperties") + @ConditionalOnProperty(name = "spring.session.store-type", havingValue = "jdbc") + public DataSourceProperties sessionDataSourceProperties() { + return new DataSourceProperties(); + } + @Bean + @Qualifier("sessionDataSource") + @SpringSessionDataSource + @ConditionalOnProperty(name = "spring.session.store-type", havingValue = "jdbc") + public HikariDataSource sessionDataSource(@Qualifier("sessionDataSourceProperties") DataSourceProperties sessionDataSourceProperties) { + return sessionDataSourceProperties + .initializeDataSourceBuilder() + .type(HikariDataSource.class) + .build(); + } +} diff --git a/iam-login-service/src/main/resources/application-jdbc-session.yml b/iam-login-service/src/main/resources/application-jdbc-session.yml new file mode 100644 index 000000000..feab805e3 --- /dev/null +++ b/iam-login-service/src/main/resources/application-jdbc-session.yml @@ -0,0 +1,26 @@ +# +# Copyright (c) Istituto Nazionale di Fisica Nucleare (INFN). 2016-2021 +# +# Licensed 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 +# +# 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. +# +spring: + session: + store-type: jdbc + jdbc: + cleanup-cron: 0 */45 * * * * + table-name: SPRING_SESSION +session: + datasource: + url: jdbc:mysql://${IAM_SESSION_DB_HOST:dev.local.io}:${IAM_SESSION_DB_PORT:3306}/${SESSION_DB_NAME:session}?useSSL=${IAM_SESSION_DB_USE_SSL:false}&allowPublicKeyRetrieval=true + username: ${SESSION_DB_USERNAME} + password: ${SESSION_DB_PASSWORD} \ No newline at end of file