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
@@ -0,0 +1,29 @@
package com.baeldung.spring.session;
import org.springframework.beans.factory.annotation.Autowired;
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;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("admin").password("password").roles("ADMIN");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.httpBasic().and()
.authorizeRequests()
.antMatchers("/").hasRole("ADMIN")
.anyRequest().authenticated();
}
}
@@ -0,0 +1,10 @@
package com.baeldung.spring.session;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer;
@Configuration
@EnableRedisHttpSession
public class SessionConfig extends AbstractHttpSessionApplicationInitializer {
}
@@ -0,0 +1,12 @@
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 helloTomcatAdmin() {
return "hello admin";
}
}
@@ -0,0 +1,11 @@
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);
}
}
@@ -0,0 +1,2 @@
spring.redis.host=localhost
spring.redis.port=6379
@@ -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);
}
}