Skip to content

Commit

Permalink
add ways to run external session store with JDBC and store session da…
Browse files Browse the repository at this point in the history
…ta in separate db
  • Loading branch information
DonaldChung-HK committed Sep 13, 2024
1 parent def2c42 commit 61cae4b
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
9 changes: 8 additions & 1 deletion iam-login-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,14 @@
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
26 changes: 26 additions & 0 deletions iam-login-service/src/main/resources/application-jdbc-session.yml
Original file line number Diff line number Diff line change
@@ -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}

0 comments on commit 61cae4b

Please sign in to comment.