Merge branch 'master' into JAVA-12732
This commit is contained in:
@@ -44,7 +44,8 @@
|
||||
<module>spring-security-web-rest</module>
|
||||
<module>spring-security-web-sockets</module>
|
||||
<module>spring-security-web-thymeleaf</module>
|
||||
<module>spring-security-web-x509</module>
|
||||
<module>spring-security-web-x509</module>
|
||||
<module>spring-social-login</module>
|
||||
<module>spring-security-opa</module>
|
||||
</modules>
|
||||
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
## Spring Session
|
||||
|
||||
This module contains articles about Spring Session
|
||||
|
||||
### Relevant Articles:
|
||||
- [Guide to Spring Session](https://www.baeldung.com/spring-session)
|
||||
- [Spring Session with JDBC](https://www.baeldung.com/spring-session-jdbc)
|
||||
- [Spring Session with MongoDB](https://www.baeldung.com/spring-session-mongodb)
|
||||
@@ -1,25 +0,0 @@
|
||||
<?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</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<name>spring-session</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
<module>spring-session-jdbc</module>
|
||||
<module>spring-session-redis</module>
|
||||
<module>spring-session-mongodb</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
@@ -1,5 +0,0 @@
|
||||
## Spring Session with JDBC
|
||||
|
||||
This module contains articles about Spring Session with JDBC.
|
||||
|
||||
### Relevant Articles:
|
||||
@@ -1,49 +0,0 @@
|
||||
<?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-jdbc</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>spring-session-jdbc</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Spring Session with JDBC tutorial</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.session</groupId>
|
||||
<artifactId>spring-session-jdbc</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>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -1,12 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
spring.datasource.generate-unique-name=false
|
||||
spring.session.store-type=jdbc
|
||||
spring.h2.console.enabled=true
|
||||
spring.h2.console.path=/h2-console
|
||||
@@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.springsessionjdbc.SpringSessionJdbcApplication;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = SpringSessionJdbcApplication.class)
|
||||
public class SpringContextTest {
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
package com.baeldung.springsessionjdbc;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.MethodSorters;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInput;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class SpringSessionJdbcIntegrationTest {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate testRestTemplate;
|
||||
|
||||
@Before
|
||||
public void setup() throws ClassNotFoundException {
|
||||
Class.forName("org.h2.Driver");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenApiHasStarted_whenH2DbIsQueried_thenSessionTablesAreEmpty() throws SQLException {
|
||||
Assert.assertEquals(0, getSessionIdsFromDatabase().size());
|
||||
Assert.assertEquals(0, getSessionAttributeBytesFromDatabase().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGetInvoked_whenH2DbIsQueried_thenOneSessionIsCreated() throws SQLException {
|
||||
assertThat(this.testRestTemplate.getForObject("http://localhost:" + port + "/", String.class)).isNotEmpty();
|
||||
Assert.assertEquals(1, getSessionIdsFromDatabase().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPostInvoked_whenH2DbIsQueried_thenSessionAttributeIsRetrieved() throws ClassNotFoundException, SQLException, IOException {
|
||||
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
|
||||
map.add("color", "red");
|
||||
this.testRestTemplate.postForObject("http://localhost:" + port + "/saveColor", map, String.class);
|
||||
List<byte[]> queryResponse = getSessionAttributeBytesFromDatabase();
|
||||
Assert.assertEquals(1, queryResponse.size());
|
||||
ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(queryResponse.get(0)));
|
||||
List<String> obj = (List<String>) in.readObject(); //Deserialize byte[] to object
|
||||
Assert.assertEquals("red", obj.get(0));
|
||||
}
|
||||
|
||||
private List<String> getSessionIdsFromDatabase() throws SQLException {
|
||||
List<String> result = new ArrayList<>();
|
||||
ResultSet rs = getResultSet("SELECT * FROM SPRING_SESSION");
|
||||
while (rs.next()) {
|
||||
result.add(rs.getString("SESSION_ID"));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<byte[]> getSessionAttributeBytesFromDatabase() throws SQLException {
|
||||
List<byte[]> result = new ArrayList<>();
|
||||
ResultSet rs = getResultSet("SELECT * FROM SPRING_SESSION_ATTRIBUTES");
|
||||
while (rs.next()) {
|
||||
result.add(rs.getBytes("ATTRIBUTE_BYTES"));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private ResultSet getResultSet(String sql) throws SQLException {
|
||||
Connection conn = DriverManager.getConnection("jdbc:h2:mem:testdb", "sa", "");
|
||||
Statement stat = conn.createStatement();
|
||||
return stat.executeQuery(sql);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
This module is for Spring Session with MONGO DB tutorial.
|
||||
Jira BAEL-2886
|
||||
|
||||
### Relevant Articles:
|
||||
@@ -1,58 +0,0 @@
|
||||
<?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-mongodb</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>spring-session-mongodb</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Spring Session with MongoDB tutorial</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.session</groupId>
|
||||
<artifactId>spring-session-data-mongodb</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-mongodb</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>de.flapdoodle.embed</groupId>
|
||||
<artifactId>de.flapdoodle.embed.mongo</artifactId>
|
||||
<version>${embed.mongo.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<embed.mongo.version>3.2.6</embed.mongo.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.baeldung.springsessionmongodb;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringSessionMongoDBApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringSessionMongoDBApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package com.baeldung.springsessionmongodb.controller;
|
||||
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
@RestController
|
||||
public class SpringSessionMongoDBController {
|
||||
|
||||
@GetMapping("/")
|
||||
public ResponseEntity<Integer> count(HttpSession session) {
|
||||
|
||||
Integer counter = (Integer) session.getAttribute("count");
|
||||
|
||||
if (counter == null) {
|
||||
counter = 1;
|
||||
} else {
|
||||
counter++;
|
||||
}
|
||||
|
||||
session.setAttribute("count", counter);
|
||||
|
||||
return ResponseEntity.ok(counter);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
spring.session.store-type=mongodb
|
||||
server.port=8080
|
||||
|
||||
spring.data.mongodb.host=localhost
|
||||
spring.data.mongodb.port=27017
|
||||
spring.data.mongodb.database=springboot-mongo
|
||||
@@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -1,16 +0,0 @@
|
||||
package com.baeldung;
|
||||
|
||||
import com.baeldung.springsessionmongodb.SpringSessionMongoDBApplication;
|
||||
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(classes = SpringSessionMongoDBApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
public class SpringContextTest {
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package com.baeldung.springsessionmongodb;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.session.data.mongo.MongoIndexedSessionRepository;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.Base64;
|
||||
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = SpringSessionMongoDBApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
public class SpringSessionMongoDBIntegrationTest {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Autowired
|
||||
private MongoIndexedSessionRepository repository;
|
||||
|
||||
private TestRestTemplate restTemplate = new TestRestTemplate();
|
||||
|
||||
@Test
|
||||
public void givenEndpointIsCalledTwiceAndResponseIsReturned_whenMongoDBIsQueriedForCount_thenCountMustBeSame() {
|
||||
HttpEntity<String> response = restTemplate
|
||||
.exchange("http://localhost:" + port, HttpMethod.GET, null, String.class);
|
||||
HttpHeaders headers = response.getHeaders();
|
||||
String set_cookie = headers.getFirst(HttpHeaders.SET_COOKIE);
|
||||
|
||||
Assert.assertEquals(response.getBody(),
|
||||
repository.findById(getSessionId(set_cookie)).getAttribute("count").toString());
|
||||
}
|
||||
|
||||
private String getSessionId(String cookie) {
|
||||
return new String(Base64.getDecoder().decode(cookie.split(";")[0].split("=")[1]));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
#To use a randomly allocated free port during tests to avoid port conflict across tests
|
||||
spring.data.mongodb.port=0
|
||||
|
||||
spring.mongodb.embedded.version=4.4.9
|
||||
@@ -1,5 +0,0 @@
|
||||
=========
|
||||
|
||||
## Spring Session Examples
|
||||
|
||||
### Relevant Articles:
|
||||
@@ -1,51 +0,0 @@
|
||||
<?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>
|
||||
<name>spring-session-redis</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../../parent-boot-2</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-data-redis</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>
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
<type>jar</type>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<embedded-redis.version>0.6</embedded-redis.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -1,15 +0,0 @@
|
||||
package com.baeldung.spring.session;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
|
||||
@Configuration
|
||||
public class PasswordEncoderConfig {
|
||||
|
||||
@Bean
|
||||
public PasswordEncoder passwordEncoder() {
|
||||
return new BCryptPasswordEncoder();
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package com.baeldung.spring.session;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
|
||||
@Autowired
|
||||
public void configureGlobal(AuthenticationManagerBuilder auth, PasswordEncoder passwordEncoder) throws Exception {
|
||||
auth.inMemoryAuthentication().withUser("admin").password(passwordEncoder.encode("password")).roles("ADMIN");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
http.httpBasic().and().authorizeRequests().antMatchers("/").hasRole("ADMIN").anyRequest().authenticated();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.baeldung.spring.session;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class SessionController {
|
||||
@RequestMapping("/")
|
||||
public String helloAdmin() {
|
||||
return "hello admin";
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package com.baeldung.spring.session;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SessionWebApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SessionWebApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
spring.redis.host=localhost
|
||||
spring.redis.port=6379
|
||||
@@ -1,13 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.spring.session.SessionWebApplication;
|
||||
|
||||
/**
|
||||
* This live test requires:
|
||||
* redis instance running on the environment
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = SessionWebApplication.class)
|
||||
public class SpringContextLiveTest {
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
package com.baeldung.spring.session;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.embedded.RedisServer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = SessionWebApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
public class SessionControllerIntegrationTest {
|
||||
|
||||
private static RedisServer redisServer;
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
private Jedis jedis;
|
||||
private TestRestTemplate testRestTemplate;
|
||||
private TestRestTemplate testRestTemplateWithAuth;
|
||||
|
||||
@BeforeClass
|
||||
public static void startRedisServer() throws IOException {
|
||||
redisServer = new RedisServer(6379);
|
||||
redisServer.start();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void stopRedisServer() {
|
||||
redisServer.stop();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void clearRedisData() {
|
||||
|
||||
testRestTemplate = new TestRestTemplate();
|
||||
testRestTemplateWithAuth = new TestRestTemplate("admin", "password");
|
||||
|
||||
jedis = new Jedis("localhost", 6379);
|
||||
jedis.flushAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRedisIsEmpty() {
|
||||
Set<String> result = jedis.keys("*");
|
||||
assertEquals(0, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnauthenticatedCantAccess() {
|
||||
ResponseEntity<String> result = testRestTemplate.getForEntity(getTestUrl(), String.class);
|
||||
assertEquals(HttpStatus.UNAUTHORIZED, result.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRedisControlsSession() {
|
||||
ResponseEntity<String> result = testRestTemplateWithAuth.getForEntity(getTestUrl(), String.class);
|
||||
assertEquals("hello admin", result.getBody()); // login worked
|
||||
|
||||
Set<String> redisResult = jedis.keys("*");
|
||||
assertTrue(redisResult.size() > 0); // redis is populated with session data
|
||||
|
||||
String sessionCookie = result.getHeaders().get("Set-Cookie").get(0).split(";")[0];
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add("Cookie", sessionCookie);
|
||||
HttpEntity<String> httpEntity = new HttpEntity<>(headers);
|
||||
|
||||
result = testRestTemplate.exchange(getTestUrl(), HttpMethod.GET, httpEntity, String.class);
|
||||
assertEquals("hello admin", result.getBody()); // access with session works worked
|
||||
|
||||
jedis.flushAll(); // clear all keys in redis
|
||||
|
||||
result = testRestTemplate.exchange(getTestUrl(), HttpMethod.GET, httpEntity, String.class);
|
||||
assertEquals(HttpStatus.UNAUTHORIZED, result.getStatusCode());// access denied after sessions are removed in redis
|
||||
}
|
||||
|
||||
private String getTestUrl(){
|
||||
return "http://localhost:" + port;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user