BAEL-89 Trimming down to one application that uses spring boot and demonstrating spring session in the unit tests.

This commit is contained in:
tschiman 2016-11-23 16:07:28 -07:00
parent 7f6130c566
commit 9cd64f8d19
15 changed files with 157 additions and 333 deletions

View File

@ -1,76 +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>jetty-session-demo</artifactId>
<version>1.0.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,12 +0,0 @@
package com.baeldung.spring.session.jettyex;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class JettyController {
@RequestMapping
public String helloJetty() {
return "hello Jetty";
}
}

View File

@ -1,19 +0,0 @@
package com.baeldung.spring.session.jettyex;
import org.springframework.context.annotation.Configuration;
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.config.http.SessionCreationPolicy;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER).and()
.authorizeRequests().anyRequest().hasRole("ADMIN");
}
}

View File

@ -1,17 +0,0 @@
package com.baeldung.spring.session.jettyex;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer;
import org.springframework.session.web.http.HeaderHttpSessionStrategy;
import org.springframework.session.web.http.HttpSessionStrategy;
@Configuration
@EnableRedisHttpSession
public class SessionConfig extends AbstractHttpSessionApplicationInitializer {
@Bean
public HttpSessionStrategy httpSessionStrategy() {
return new HeaderHttpSessionStrategy();
}
}

View File

@ -1,3 +0,0 @@
server.port=8081
spring.redis.host=localhost
spring.redis.port=6379

View File

@ -4,19 +4,68 @@
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>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>spring-session</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>jetty-session-demo</module>
<module>tomcat-session-demo</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,4 +1,4 @@
package com.baeldung.spring.session.tomcatex;
package com.baeldung.spring.session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
@ -23,7 +23,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
http
.httpBasic().and()
.authorizeRequests()
.antMatchers("/tomcat/admin").hasRole("ADMIN")
.antMatchers("/").hasRole("ADMIN")
.anyRequest().authenticated();
}
}

View File

@ -1,4 +1,4 @@
package com.baeldung.spring.session.tomcatex;
package com.baeldung.spring.session;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

View File

@ -1,12 +1,12 @@
package com.baeldung.spring.session.tomcatex;
package com.baeldung.spring.session;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TomcatController {
@RequestMapping("/tomcat/admin")
public class SessionController {
@RequestMapping("/")
public String helloTomcatAdmin() {
return "hello tomcat admin";
return "hello admin";
}
}

View File

@ -1,11 +1,11 @@
package com.baeldung.spring.session.jettyex;
package com.baeldung.spring.session;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class JettyWebApplication {
public class SessionWebApplication {
public static void main(String[] args) {
SpringApplication.run(JettyWebApplication.class, args);
SpringApplication.run(SessionWebApplication.class, args);
}
}

View File

@ -0,0 +1,87 @@
package com.baeldung.spring.session;
import org.apache.tomcat.util.codec.binary.Base64;
import org.junit.Before;
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.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.http.*;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SessionControllerTest {
@Autowired
private TestRestTemplate restTemplate;
@Autowired
private JedisConnectionFactory jedisConnectionFactory;
private RedisConnection connection;
@Before
public void clearRedisData() {
connection = jedisConnectionFactory.getConnection();
connection.flushAll();
}
@Test
public void testRedisIsEmpty() {
Set<byte[]> result = connection.keys("*".getBytes());
assertEquals(0, result.size());
}
@Test
public void testUnauthenticatedCantAccess() {
ResponseEntity<String> result = restTemplate.getForEntity("/", String.class);
assertEquals(HttpStatus.UNAUTHORIZED, result.getStatusCode());
}
@Test
public void testRedisControlsSession() {
ResponseEntity<String> result = restTemplate.exchange("/", HttpMethod.GET, makeAuthRequest(), String.class);
assertEquals("hello admin", result.getBody()); //login worked
Set<byte[]> redisResult = connection.keys("*".getBytes());
assertTrue(redisResult.size() > 0); //redis is populated with session data
String sessionCookie = result.getHeaders().get("Set-Cookie").get(0).split(";")[0];
result = restTemplate.exchange("/", HttpMethod.GET, makeRequestWithCookie(sessionCookie), String.class);
assertEquals("hello admin", result.getBody()); //access with session works worked
connection.flushAll(); //clear all keys in redis
result = restTemplate.exchange("/", HttpMethod.GET, makeRequestWithCookie(sessionCookie), String.class);
assertEquals(HttpStatus.UNAUTHORIZED, result.getStatusCode());//access denied after sessions are removed in redis
}
private HttpEntity<String> makeRequestWithCookie(String sessionCookie) {
HttpHeaders headers = new HttpHeaders();
headers.add("Cookie", sessionCookie);
return new HttpEntity<>(headers);
}
private HttpEntity<String> makeAuthRequest() {
String plainCreds = "admin:password";
byte[] plainCredsBytes = plainCreds.getBytes();
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
String base64Creds = new String(base64CredsBytes);
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);
return new HttpEntity<>(headers);
}
}

View File

@ -1,71 +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>tomcat-session-demo</artifactId>
<version>1.0.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

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

View File

@ -1,103 +0,0 @@
package com.baeldung.spring.session.tomcatex;
import org.apache.tomcat.util.codec.binary.Base64;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.http.*;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TomcatControllerTest {
@Autowired
private TestRestTemplate restTemplate;
@LocalServerPort
private int port;
@Autowired
private JedisConnectionFactory jedisConnectionFactory;
private RedisConnection connection;
@Before
public void clearRedisData() {
connection = jedisConnectionFactory.getConnection();
connection.flushAll();
}
@Test
public void testRedisIsEmpty() {
Set<byte[]> result = connection.keys("*".getBytes());
assertEquals(0, result.size());
}
@Test
public void testForbiddenToProtectedEndpoint() {
ResponseEntity<String> result = restTemplate.getForEntity("/tomcat/admin", String.class);
assertEquals(HttpStatus.UNAUTHORIZED, result.getStatusCode());
}
@Test
public void testLoginAddsRedisKey() {
ResponseEntity<String> result = makeRequest();
assertEquals("hello tomcat admin", result.getBody()); //login worked
Set<byte[]> redisResult = connection.keys("*".getBytes());
assertTrue(redisResult.size() > 0); //redis was populated with data
}
@Test //requires that the jetty service is running on port 8081
public void testFailureAccessingJettyResourceWithTomcatSessionToken() {
//call the jetty server with the token
ResponseEntity<String> jettyResult = restTemplate.getForEntity("http://localhost:8081", String.class);
assertEquals(HttpStatus.UNAUTHORIZED, jettyResult.getStatusCode()); //login worked
}
@Test //requires that the jetty service is running on port 8081
public void testAccessingJettyResourceWithTomcatSessionToken() {
//login to get a session token
ResponseEntity<String> result = makeRequest();
assertEquals("hello tomcat admin", result.getBody()); //login worked
assertTrue(result.getHeaders().containsKey("Set-Cookie"));
String setCookieValue = result.getHeaders().get("Set-Cookie").get(0);
String sessionCookie = setCookieValue.split(";")[0];
String sessionValue = sessionCookie.split("=")[1];
//Add session token to headers
HttpHeaders headers = new HttpHeaders();
headers.add("x-auth-token", sessionValue);
//call the jetty server with the token
HttpEntity<String> request = new HttpEntity<>(headers);
ResponseEntity<String> jettyResult = restTemplate.exchange("http://localhost:8081", HttpMethod.GET, request, String.class);
assertEquals("hello Jetty", jettyResult.getBody()); //login worked
}
private ResponseEntity<String> makeRequest() {
String plainCreds = "admin:password";
byte[] plainCredsBytes = plainCreds.getBytes();
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
String base64Creds = new String(base64CredsBytes);
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);
HttpEntity<String> request = new HttpEntity<>(headers);
return restTemplate.exchange("http://localhost:" + port + "/tomcat/admin", HttpMethod.GET, request, String.class);
}
}