1
1
package sample ;
2
2
3
- import java .sql .Types ;
4
3
import java .util .Base64 ;
5
- import java .util .List ;
6
- import java .util .Map ;
7
4
8
5
import com .fasterxml .jackson .databind .ObjectMapper ;
9
6
import jakarta .servlet .http .Cookie ;
10
- import org .assertj .core .api .Assertions ;
11
7
import org .junit .jupiter .api .BeforeEach ;
12
8
import org .junit .jupiter .api .Test ;
13
9
@@ -51,11 +47,20 @@ void setup() {
51
47
52
48
@ Test
53
49
void loginShouldSaveSecurityContextAsJson () throws Exception {
54
- Cookie sessionCookie = this .mvc .perform (formLogin ().user ("rüdiger" ).password ("password" ))
55
- .andExpect (authenticated ())
56
- .andReturn ()
57
- .getResponse ()
58
- .getCookie ("SESSION" );
50
+ loginShouldSaveSecurityContextAsJson ("user" );
51
+ }
52
+
53
+ @ Test
54
+ void loginShouldSaveSecurityContextAsJsonWhenSpecialCharacters () throws Exception {
55
+ loginShouldSaveSecurityContextAsJson ("rüdiger" );
56
+ }
57
+
58
+ private void loginShouldSaveSecurityContextAsJson (String username ) throws Exception {
59
+ Cookie sessionCookie = this .mvc .perform (formLogin ().user (username ).password ("password" ))
60
+ .andExpect (authenticated ())
61
+ .andReturn ()
62
+ .getResponse ()
63
+ .getCookie ("SESSION" );
59
64
String sessionId = new String (Base64 .getDecoder ().decode (sessionCookie .getValue ()));
60
65
Object attributeBytes = this .jdbcClient .sql ("""
61
66
SELECT attribute_bytes::text FROM spring_session_attributes
@@ -66,20 +71,29 @@ void loginShouldSaveSecurityContextAsJson() throws Exception {
66
71
SecurityContext securityContext = this .objectMapperWithModules .readValue ((String ) attributeBytes ,
67
72
SecurityContext .class );
68
73
assertThat (securityContext ).isNotNull ();
69
- assertThat (securityContext .getAuthentication ().getName ()).isEqualTo ("rüdiger" );
74
+ assertThat (securityContext .getAuthentication ().getName ()).isEqualTo (username );
70
75
}
71
76
72
77
@ Test
73
78
void loginWhenQueryUsingJsonbOperatorThenReturns () throws Exception {
74
- this .mvc .perform (formLogin ().user ("rüdiger" ).password ("password" )).andExpect (authenticated ());
79
+ loginWhenQueryUsingJsonbOperatorThenReturns ("user" );
80
+ }
81
+
82
+ @ Test
83
+ void loginWhenQueryUsingJsonbOperatorThenReturnsWhenSpecialCharacters () throws Exception {
84
+ loginWhenQueryUsingJsonbOperatorThenReturns ("rüdiger" );
85
+ }
86
+
87
+ private void loginWhenQueryUsingJsonbOperatorThenReturns (String username ) throws Exception {
88
+ this .mvc .perform (formLogin ().user (username ).password ("password" )).andExpect (authenticated ());
75
89
Object attributeBytes = this .jdbcClient .sql ("""
76
90
SELECT attribute_bytes::text FROM spring_session_attributes
77
- WHERE attribute_bytes -> 'authentication' -> 'principal' ->> 'username' = 'rüdiger '
78
- """ ).query ().singleValue ();
91
+ WHERE attribute_bytes -> 'authentication' -> 'principal' ->> 'username' = '%s '
92
+ """ . formatted ( username ) ).query ().singleValue ();
79
93
SecurityContext securityContext = this .objectMapperWithModules .readValue ((String ) attributeBytes ,
80
94
SecurityContext .class );
81
95
assertThat (securityContext ).isNotNull ();
82
- assertThat (securityContext .getAuthentication ().getName ()).isEqualTo ("rüdiger" );
96
+ assertThat (securityContext .getAuthentication ().getName ()).isEqualTo (username );
83
97
}
84
98
85
99
}
0 commit comments