BAEL-1911 - Spring session with JDBC (#4689)

This commit is contained in:
Aravind Ranganathan 2018-07-12 03:10:53 -04:00 committed by Predrag Maric
parent cddf858c05
commit 6335e28a9e
17 changed files with 232 additions and 29 deletions

View File

@ -4,3 +4,4 @@
### Relevant Articles:
- [Introduction to Spring Session](http://www.baeldung.com/spring-session)
- [Spring Session with JDBC](http://www.baeldung.com)

View File

@ -1,10 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung</groupId>
<artifactId>spring-session</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-session</name>
<packaging>pom</packaging>
<parent>
<artifactId>parent-boot-1</artifactId>
@ -13,32 +15,9 @@
<relativePath>../parent-boot-1</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.github.kstyrc</groupId>
<artifactId>embedded-redis</artifactId>
<version>${embedded-redis.version}</version>
</dependency>
</dependencies>
<properties>
<embedded-redis.version>0.6</embedded-redis.version>
</properties>
<modules>
<module>spring-session-jdbc</module>
<module>spring-session-redis</module>
</modules>
</project>

View File

@ -0,0 +1,6 @@
This module is for Spring Session with JDBC tutorial.
Jira BAEL-1911
### Relevant Articles:
- [Spring Session with JDBC](http://www.baeldung.com)

View File

@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>spring-session-jdbc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-session-jdbc</name>
<description>Spring Session with JDBC tutorial</description>
<parent>
<artifactId>parent-boot-2</artifactId>
<groupId>com.baeldung</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-jdbc</artifactId>
</dependency>
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-security</artifactId>-->
<!--</dependency>-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,12 @@
package com.baeldung.springsessionjdbc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringSessionJdbcApplication {
public static void main(String[] args) {
SpringApplication.run(SpringSessionJdbcApplication.class, args);
}
}

View File

@ -0,0 +1,46 @@
package com.baeldung.springsessionjdbc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
@Controller
public class SpringSessionJdbcController {
@GetMapping("/")
public String index(Model model, HttpSession session) {
List<String> favoriteColors = getFavColors(session);
model.addAttribute("favoriteColors", favoriteColors);
model.addAttribute("sessionId", session.getId());
return "index";
}
@PostMapping("/saveColor")
public String saveMessage(@RequestParam("color") String color,
HttpServletRequest request) {
List<String> favoriteColors = getFavColors(request.getSession());
if (!StringUtils.isEmpty(color)) {
favoriteColors.add(color);
request.getSession().
setAttribute("favoriteColors", favoriteColors);
}
return "redirect:/";
}
private List<String> getFavColors(HttpSession session) {
List<String> favoriteColors = (List<String>) session.
getAttribute("favoriteColors");
if (favoriteColors == null) {
favoriteColors = new ArrayList<>();
}
return favoriteColors;
}
}

View File

@ -0,0 +1,9 @@
spring.session.store-type=jdbc
#spring.session.jdbc.initialize-schema=embedded
#spring.session.jdbc.table-name=SPRING_SESSION
#server.servlet.session.timeout=60s
#spring.datasource.url=jdbc:h2:mem:AZ
#spring.security.user.name=admin
#spring.security.user.password=secret
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

View File

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Spring Session JDBC</title>
</head>
<body>
<div>
<form th:action="@{/saveColor}" method="post">
<input name="color" type="text" placeholder="Enter your favorite color"/>
<input type="submit" value="Save"/>
</form>
</div>
<p>Session ID - <span th:text="${sessionId}"/></p>
<p>My favorite color(s) - <span th:text="${favoriteColors}"/></p>
</body>
</html>

View File

@ -0,0 +1,16 @@
package com.baeldung.springsessionjdbc;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringSessionJdbcApplicationTests {
@Test
public void contextLoads() {
}
}

View File

@ -0,0 +1,6 @@
=========
## Spring Session Examples
### Relevant Articles:
- [Introduction to Spring Session](http://www.baeldung.com/spring-session)

View File

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-session-redis</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<artifactId>parent-boot-1</artifactId>
<groupId>com.baeldung</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-1</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.github.kstyrc</groupId>
<artifactId>embedded-redis</artifactId>
<version>${embedded-redis.version}</version>
</dependency>
</dependencies>
<properties>
<embedded-redis.version>0.6</embedded-redis.version>
</properties>
</project>