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
14
10
import org .springframework .beans .factory .annotation .Autowired ;
15
11
import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
16
12
import org .springframework .boot .test .context .SpringBootTest ;
17
13
import org .springframework .context .annotation .Import ;
18
- import org .springframework .jdbc .core .simple . JdbcClient ;
14
+ import org .springframework .jdbc .core .JdbcTemplate ;
19
15
import org .springframework .security .core .context .SecurityContext ;
20
16
import org .springframework .security .jackson2 .SecurityJackson2Modules ;
21
17
import org .springframework .test .web .servlet .MockMvc ;
@@ -38,15 +34,15 @@ public class JdbcJsonAttributeTests {
38
34
ObjectMapper objectMapperWithModules ;
39
35
40
36
@ Autowired
41
- JdbcClient jdbcClient ;
37
+ JdbcTemplate jdbcClient ;
42
38
43
39
@ BeforeEach
44
40
void setup () {
45
41
ObjectMapper copy = this .objectMapper .copy ();
46
42
copy .registerModules (SecurityJackson2Modules .getModules (getClass ().getClassLoader ()));
47
43
this .objectMapperWithModules = copy ;
48
- this .jdbcClient .sql ("DELETE FROM spring_session_attributes" ). update ( );
49
- this .jdbcClient .sql ("DELETE FROM spring_session" ). update ( );
44
+ this .jdbcClient .execute ("DELETE FROM spring_session_attributes" );
45
+ this .jdbcClient .execute ("DELETE FROM spring_session" );
50
46
}
51
47
52
48
@ Test
@@ -57,12 +53,12 @@ void loginShouldSaveSecurityContextAsJson() throws Exception {
57
53
.getResponse ()
58
54
.getCookie ("SESSION" );
59
55
String sessionId = new String (Base64 .getDecoder ().decode (sessionCookie .getValue ()));
60
- Object attributeBytes = this .jdbcClient .sql ("""
56
+ Object attributeBytes = this .jdbcClient .queryForObject ("""
61
57
SELECT attribute_bytes::text FROM spring_session_attributes
62
58
INNER JOIN spring_session s ON s.primary_id = session_primary_id
63
59
WHERE attribute_name = 'SPRING_SECURITY_CONTEXT'
64
- AND s.session_id = :id
65
- """ ). param ( "id" , sessionId ). query (). singleValue ( );
60
+ AND s.session_id = ?
61
+ """ , Object . class , sessionId );
66
62
SecurityContext securityContext = this .objectMapperWithModules .readValue ((String ) attributeBytes ,
67
63
SecurityContext .class );
68
64
assertThat (securityContext ).isNotNull ();
@@ -72,10 +68,10 @@ void loginShouldSaveSecurityContextAsJson() throws Exception {
72
68
@ Test
73
69
void loginWhenQueryUsingJsonbOperatorThenReturns () throws Exception {
74
70
this .mvc .perform (formLogin ().user ("user" ).password ("password" )).andExpect (authenticated ());
75
- Object attributeBytes = this .jdbcClient .sql ("""
71
+ Object attributeBytes = this .jdbcClient .queryForObject ("""
76
72
SELECT attribute_bytes::text FROM spring_session_attributes
77
73
WHERE attribute_bytes -> 'authentication' -> 'principal' ->> 'username' = 'user'
78
- """ ). query (). singleValue ( );
74
+ """ , Object . class );
79
75
SecurityContext securityContext = this .objectMapperWithModules .readValue ((String ) attributeBytes ,
80
76
SecurityContext .class );
81
77
assertThat (securityContext ).isNotNull ();
0 commit comments