Skip to content

Commit 8b97a32

Browse files
jitendra-bishtRob Winch
authored andcommitted
Mix-ins added for Jackson Serialization/deserialization
Fixes gh-434
1 parent d337902 commit 8b97a32

34 files changed

+8570
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
buildscript {
2+
repositories {
3+
mavenCentral()
4+
}
5+
dependencies {
6+
classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion")
7+
}
8+
}
9+
10+
apply plugin: 'spring-boot'
11+
12+
apply from: JAVA_GRADLE
13+
14+
//tasks.findByPath("artifactoryPublish")?.enabled = false
15+
16+
group = 'samples'
17+
ext {
18+
jsonassertVersion="1.3.0"
19+
assertjVersion = "2.4.0"
20+
}
21+
22+
dependencies {
23+
compile project(':spring-session'),
24+
"org.springframework.boot:spring-boot-starter-redis",
25+
"org.springframework.boot:spring-boot-starter-web",
26+
"org.springframework.boot:spring-boot-starter-thymeleaf",
27+
"nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect",
28+
"org.springframework.security:spring-security-web:$springSecurityVersion",
29+
"org.springframework.security:spring-security-core:$springSecurityVersion",
30+
"org.springframework.security:spring-security-config:$springSecurityVersion",
31+
"org.apache.httpcomponents:httpclient"
32+
33+
testCompile "org.springframework.boot:spring-boot-starter-test",
34+
"org.assertj:assertj-core:$assertjVersion"
35+
testCompile "org.skyscreamer:jsonassert:$jsonassertVersion"
36+
testCompile "org.assertj:assertj-core:$assertjVersion"
37+
integrationTestCompile gebDependencies,
38+
"org.spockframework:spock-spring:$spockVersion"
39+
40+
}
41+
//
42+
integrationTest {
43+
doFirst {
44+
def port = reservePort()
45+
46+
def host = 'localhost:' + port
47+
systemProperties['geb.build.baseUrl'] = 'http://'+host+'/'
48+
systemProperties['geb.build.reportsDir'] = 'build/geb-reports'
49+
systemProperties['server.port'] = port
50+
systemProperties['management.port'] = 0
51+
52+
systemProperties['spring.session.redis.namespace'] = project.name
53+
}
54+
jvmArgs "-XX:-UseSplitVerifier"
55+
}
56+
57+
integrationTest {
58+
testLogging {
59+
events "passed", "skipped", "failed"
60+
}
61+
}
62+
63+
def reservePort() {
64+
def socket = new ServerSocket(0)
65+
def result = socket.localPort
66+
socket.close()
67+
result
68+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package samples
2+
3+
import geb.spock.GebSpec
4+
import org.springframework.boot.test.IntegrationTest
5+
import org.springframework.boot.test.SpringApplicationContextLoader
6+
import org.springframework.test.context.ContextConfiguration
7+
import org.springframework.test.context.web.WebAppConfiguration
8+
import sample.Application
9+
import samples.pages.HomePage
10+
import samples.pages.LoginPage
11+
import samples.pages.SetAttributePage
12+
import spock.lang.Stepwise
13+
14+
/**
15+
* @author jitendra on 15/3/16.
16+
*/
17+
@Stepwise
18+
@ContextConfiguration(classes = Application, loader = SpringApplicationContextLoader)
19+
@WebAppConfiguration
20+
@IntegrationTest
21+
class HttpRedisJsonTest extends GebSpec {
22+
23+
def 'login page test'() {
24+
when:
25+
to LoginPage
26+
then:
27+
at LoginPage
28+
}
29+
30+
def "Unauthenticated user sent to login page"() {
31+
when:
32+
via HomePage
33+
then:
34+
at LoginPage
35+
}
36+
37+
def "Successful Login test"() {
38+
when:
39+
login()
40+
then:
41+
at HomePage
42+
driver.manage().cookies.find {it.name == "SESSION"}
43+
!driver.manage().cookies.find {it.name == "JSESSIONID"}
44+
}
45+
46+
def "Set and get attributes in session"() {
47+
when:
48+
setAttribute("Demo Key", "Demo Value")
49+
50+
then:
51+
at SetAttributePage
52+
tdKey()*.text().contains("Demo Key")
53+
tdKey()*.text().contains("Demo Value")
54+
}
55+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package samples.pages
2+
3+
import geb.Page
4+
5+
/**
6+
* @author jitendra on 15/3/16.
7+
*/
8+
class HomePage extends Page {
9+
static url = "/"
10+
11+
static at = {
12+
driver.title == "Spring Session Sample - Home"
13+
}
14+
15+
static content = {
16+
form { $('form') }
17+
submit { $('button[type=submit]') }
18+
setAttribute(required: false) { key = 'project', value = 'SessionRedisJson' ->
19+
form.key = key
20+
form.value = value
21+
submit.click(SetAttributePage)
22+
}
23+
}
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package samples.pages
2+
3+
import geb.Page
4+
5+
/**
6+
* @author jitendra on 15/3/16.
7+
*/
8+
class LoginPage extends Page {
9+
static url = "/login"
10+
11+
static at = {
12+
assert title == "Spring Session Sample - Login"
13+
return true
14+
}
15+
16+
static content = {
17+
form { $('form') }
18+
submit { $('button[type=submit]') }
19+
login(required: false) { user = 'user', pass = 'password' ->
20+
form.username = user
21+
form.password = pass
22+
submit.click(HomePage)
23+
}
24+
}
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package samples.pages
2+
3+
import geb.Page
4+
5+
/**
6+
* @author jitendra on 15/3/16.
7+
*/
8+
class SetAttributePage extends Page {
9+
static url = "/setValue"
10+
11+
static at = {
12+
title == "Spring Session Sample - Home"
13+
}
14+
15+
static content = {
16+
tdKey { $('td') }
17+
}
18+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package samples;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.SpringApplicationConfiguration;
7+
import org.springframework.data.redis.core.RedisTemplate;
8+
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
9+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10+
import sample.Application;
11+
12+
import static org.springframework.util.Assert.notNull;
13+
import static org.junit.Assert.*;
14+
15+
/**
16+
* @author jitendra on 8/3/16.
17+
*/
18+
@RunWith(SpringJUnit4ClassRunner.class)
19+
@SpringApplicationConfiguration(Application.class)
20+
public class RedisSerializerTest {
21+
22+
@Autowired
23+
RedisTemplate sessionRedisTemplate;
24+
25+
@Test
26+
public void testRedisTemplate() {
27+
notNull(sessionRedisTemplate);
28+
notNull(sessionRedisTemplate.getDefaultSerializer());
29+
assertTrue(sessionRedisTemplate.getDefaultSerializer() instanceof GenericJackson2JsonRedisSerializer);
30+
}
31+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package samples.mixins;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.SpringApplicationConfiguration;
7+
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
8+
import org.springframework.data.redis.serializer.SerializationException;
9+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
10+
import org.springframework.security.core.authority.SimpleGrantedAuthority;
11+
import org.springframework.security.core.userdetails.User;
12+
import org.springframework.security.web.authentication.WebAuthenticationDetails;
13+
import org.springframework.security.web.csrf.DefaultCsrfToken;
14+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
15+
import sample.Application;
16+
17+
import javax.servlet.http.Cookie;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
/**
22+
* @author jitendra on 28/3/16.
23+
*/
24+
@RunWith(SpringJUnit4ClassRunner.class)
25+
@SpringApplicationConfiguration(Application.class)
26+
public class MixinsDeserilizeTest {
27+
28+
@Autowired
29+
GenericJackson2JsonRedisSerializer redisSerializer;
30+
31+
@Test
32+
public void defaultCsrfTokenMixin() {
33+
String tokenJson = "{\"@class\": \"org.springframework.security.web.csrf.DefaultCsrfToken\", \"token\": \"123456\", \"parameterName\": \"_csrf\", \"headerName\": \"x-csrf-header\"}";
34+
DefaultCsrfToken token = redisSerializer.deserialize(tokenJson.getBytes(), DefaultCsrfToken.class);
35+
assertThat(token)
36+
.hasFieldOrPropertyWithValue("token", "123456")
37+
.hasFieldOrPropertyWithValue("parameterName", "_csrf")
38+
.hasFieldOrPropertyWithValue("headerName", "x-csrf-header");
39+
}
40+
41+
@Test
42+
public void httpCookieTest() {
43+
String httpCookie = "{\"@class\": \"javax.servlet.http.Cookie\", \"name\": \"SESSION\", \"value\": \"123456789\", \"maxAge\": 1000, \"path\": \"/\", \"secure\": true, \"version\": 0, \"httpOnly\": true}";
44+
Cookie cookie = redisSerializer.deserialize(httpCookie.getBytes(), Cookie.class);
45+
assertThat(cookie).hasFieldOrPropertyWithValue("name", "SESSION")
46+
.hasFieldOrPropertyWithValue("value", "123456789")
47+
.hasFieldOrPropertyWithValue("secure", true)
48+
.hasFieldOrPropertyWithValue("comment", "")
49+
.hasFieldOrPropertyWithValue("path", "/")
50+
.hasFieldOrPropertyWithValue("maxAge", 1000)
51+
.hasFieldOrPropertyWithValue("httpOnly", true);
52+
}
53+
54+
@Test(expected = SerializationException.class)
55+
public void simpleGrantedAuthorityWithoutTypeIdTest() {
56+
String authorityJson = "{\"authority\": \"ROLE_USER\"}";
57+
SimpleGrantedAuthority authority = redisSerializer.deserialize(authorityJson.getBytes(), SimpleGrantedAuthority.class);
58+
assertThat(authority.getAuthority()).isEqualTo("ROLE_USER");
59+
}
60+
61+
@Test
62+
public void simpleGrantedAuthorityWithTypeIdTest() {
63+
String authorityJson = "{\"@class\": \"org.springframework.security.core.authority.SimpleGrantedAuthority\", \"role\": \"ROLE_USER\"}";
64+
SimpleGrantedAuthority authority = redisSerializer.deserialize(authorityJson.getBytes(), SimpleGrantedAuthority.class);
65+
assertThat(authority.getAuthority()).isEqualTo("ROLE_USER");
66+
}
67+
68+
@Test
69+
public void userTest() {
70+
String userJson = "{\"@class\": \"org.springframework.security.core.userdetails.User\", \"username\": \"user\", \"password\": \"password\", \"authorities\": [\"java.util.Collections$UnmodifiableSet\", [{\"@class\": \"org.springframework.security.core.authority.SimpleGrantedAuthority\", \"role\": \"ROLE_USER\"}]], \"accountNonExpired\": true, \"accountNonLocked\": true, \"credentialsNonExpired\": true, \"enabled\": true}";
71+
User user = redisSerializer.deserialize(userJson.getBytes(), User.class);
72+
assertThat(user.getUsername()).isEqualTo("user");
73+
assertThat(user.getPassword()).isEqualTo("password");
74+
assertThat(user.getAuthorities()).contains(new SimpleGrantedAuthority("ROLE_USER"));
75+
assertThat(user.isEnabled()).isEqualTo(true);
76+
assertThat(user.isAccountNonExpired()).isEqualTo(true);
77+
assertThat(user.isAccountNonLocked()).isEqualTo(true);
78+
assertThat(user.isCredentialsNonExpired()).isEqualTo(true);
79+
}
80+
81+
@Test
82+
public void unauthenticatedUsernamePasswordAuthenticationTokenTest() {
83+
String unauthenticatedTokenJson = "{\"@class\": \"org.springframework.security.authentication.UsernamePasswordAuthenticationToken\"," +
84+
"\"principal\": \"user\", \"credentials\": \"password\", \"details\": null, \"authorities\": [\"java.util.ArrayList\", []]," +
85+
"\"authenticated\": false}";
86+
UsernamePasswordAuthenticationToken token = redisSerializer.deserialize(unauthenticatedTokenJson.getBytes(), UsernamePasswordAuthenticationToken.class);
87+
assertThat(token.getPrincipal()).isEqualTo("user");
88+
assertThat(token.getCredentials()).isEqualTo("password");
89+
assertThat(token.isAuthenticated()).isEqualTo(false);
90+
assertThat(token.getAuthorities()).hasSize(0);
91+
}
92+
93+
@Test
94+
public void unauthenticatedUsernamePasswordAuthenticationTokenWithUserAsPrincipalTest() {
95+
String unauthenticatedTokenJson = "{\"@class\": \"org.springframework.security.authentication.UsernamePasswordAuthenticationToken\"," +
96+
"\"principal\": {\"@class\": \"org.springframework.security.core.userdetails.User\", \"username\": \"user\", \"password\": \"password\", " +
97+
"\"authorities\": [\"java.util.Collections$UnmodifiableSet\", [{\"@class\": \"org.springframework.security.core.authority.SimpleGrantedAuthority\"," +
98+
" \"role\": \"ROLE_USER\"}]], \"accountNonExpired\": true, \"accountNonLocked\": true, \"credentialsNonExpired\": true, \"enabled\": true}, " +
99+
"\"credentials\": \"password\", \"details\": null, \"authorities\": [\"java.util.ArrayList\", []], \"authenticated\": false}";
100+
UsernamePasswordAuthenticationToken token = redisSerializer.deserialize(unauthenticatedTokenJson.getBytes(), UsernamePasswordAuthenticationToken.class);
101+
assertThat(token.getPrincipal()).isInstanceOf(User.class);
102+
User user = (User) token.getPrincipal();
103+
assertThat(user.getUsername()).isEqualTo("user");
104+
assertThat(user.getPassword()).isEqualTo("password");
105+
assertThat(user.getAuthorities()).contains(new SimpleGrantedAuthority("ROLE_USER"));
106+
assertThat(user.isEnabled()).isEqualTo(true);
107+
assertThat(user.isAccountNonExpired()).isEqualTo(true);
108+
assertThat(user.isAccountNonLocked()).isEqualTo(true);
109+
assertThat(user.isCredentialsNonExpired()).isEqualTo(true);
110+
}
111+
112+
@Test
113+
public void authenticatedUsernamePasswordAuthenticationTokenTest() {
114+
String unauthenticatedTokenJson = "{\"@class\": \"org.springframework.security.authentication.UsernamePasswordAuthenticationToken\"," +
115+
"\"principal\": \"user\", \"credentials\": \"password\", \"details\": null, \"authorities\": [\"java.util.ArrayList\", " +
116+
"[{\"@class\": \"org.springframework.security.core.authority.SimpleGrantedAuthority\", \"role\": \"ROLE_USER\"}]]," +
117+
"\"authenticated\": true}";
118+
UsernamePasswordAuthenticationToken authenticationToken = redisSerializer.deserialize(unauthenticatedTokenJson.getBytes(), UsernamePasswordAuthenticationToken.class);
119+
assertThat(authenticationToken.getPrincipal()).isEqualTo("user");
120+
assertThat(authenticationToken.getCredentials()).isEqualTo("password");
121+
assertThat(authenticationToken.isAuthenticated()).isEqualTo(true);
122+
assertThat(authenticationToken.getAuthorities()).hasSize(1);
123+
assertThat(authenticationToken.getAuthorities()).contains(new SimpleGrantedAuthority("ROLE_USER"));
124+
}
125+
126+
@Test
127+
public void webAuthenticationDetailTest() {
128+
String authenticationDetailJson = "{\"@class\": \"org.springframework.security.web.authentication.WebAuthenticationDetails\"," +
129+
"\"remoteAddress\": \"http://localhost/login\", \"sessionId\": \"123456789\"}";
130+
WebAuthenticationDetails details = redisSerializer.deserialize(authenticationDetailJson.getBytes(), WebAuthenticationDetails.class);
131+
assertThat(details.getRemoteAddress()).isEqualTo("http://localhost/login");
132+
assertThat(details.getSessionId()).isEqualTo("123456789");
133+
}
134+
}

0 commit comments

Comments
 (0)