13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
- package org .springframework .security .web .bind . support ;
16
+ package org .springframework .security .web .method . annotation ;
17
17
18
18
import java .lang .annotation .ElementType ;
19
19
import java .lang .annotation .Retention ;
45
45
*
46
46
*/
47
47
public class CurrentSecurityContextArgumentResolverTests {
48
- private Object expectedPrincipal ;
49
48
private CurrentSecurityContextArgumentResolver resolver ;
50
49
51
50
@ Before
52
51
public void setup () {
53
- resolver = new CurrentSecurityContextArgumentResolver ();
52
+ this . resolver = new CurrentSecurityContextArgumentResolver ();
54
53
}
55
54
56
55
@ After
@@ -60,45 +59,48 @@ public void cleanup() {
60
59
61
60
@ Test
62
61
public void supportsParameterNoAnnotation () {
63
- assertThat (resolver .supportsParameter (showSecurityContextNoAnnotation ())).isFalse ();
62
+ assertThat (this . resolver .supportsParameter (showSecurityContextNoAnnotation ())).isFalse ();
64
63
}
65
64
66
65
@ Test
67
66
public void supportsParameterAnnotation () {
68
- assertThat (resolver .supportsParameter (showSecurityContextAnnotation ())).isTrue ();
67
+ assertThat (this . resolver .supportsParameter (showSecurityContextAnnotation ())).isTrue ();
69
68
}
70
69
71
70
@ Test
72
- public void resolveArgumentWithCustomSecurityContext () throws Exception {
71
+ public void resolveArgumentWithCustomSecurityContext () {
73
72
String principal = "custom_security_context" ;
74
73
setAuthenticationPrincipalWithCustomSecurityContext (principal );
75
- CustomSecurityContext customSecurityContext = (CustomSecurityContext ) resolver .resolveArgument (showAnnotationWithCustomSecurityContext (), null , null , null );
74
+ CustomSecurityContext customSecurityContext = (CustomSecurityContext )
75
+ this .resolver .resolveArgument (showAnnotationWithCustomSecurityContext (), null , null , null );
76
76
assertThat (customSecurityContext .getAuthentication ().getPrincipal ()).isEqualTo (principal );
77
77
}
78
78
79
79
@ Test
80
- public void resolveArgumentWithCustomSecurityContextTypeMatch () throws Exception {
80
+ public void resolveArgumentWithCustomSecurityContextTypeMatch () {
81
81
String principal = "custom_security_context_type_match" ;
82
82
setAuthenticationPrincipalWithCustomSecurityContext (principal );
83
- CustomSecurityContext customSecurityContext = (CustomSecurityContext ) resolver .resolveArgument (showAnnotationWithCustomSecurityContext (), null , null , null );
83
+ CustomSecurityContext customSecurityContext = (CustomSecurityContext )
84
+ this .resolver .resolveArgument (showAnnotationWithCustomSecurityContext (), null , null , null );
84
85
assertThat (customSecurityContext .getAuthentication ().getPrincipal ()).isEqualTo (principal );
85
86
}
86
87
87
88
@ Test
88
- public void resolveArgumentNullAuthentication () throws Exception {
89
+ public void resolveArgumentNullAuthentication () {
89
90
SecurityContext context = SecurityContextHolder .getContext ();
90
91
Authentication authentication = context .getAuthentication ();
91
92
context .setAuthentication (null );
92
- assertThat (resolver .resolveArgument (showSecurityContextAuthenticationAnnotation (), null , null , null ))
93
+ assertThat (this . resolver .resolveArgument (showSecurityContextAuthenticationAnnotation (), null , null , null ))
93
94
.isNull ();
94
95
context .setAuthentication (authentication );
95
96
}
96
97
97
98
@ Test
98
- public void resolveArgumentWithAuthentication () throws Exception {
99
+ public void resolveArgumentWithAuthentication () {
99
100
String principal = "john" ;
100
101
setAuthenticationPrincipal (principal );
101
- Authentication auth1 = (Authentication ) resolver .resolveArgument (showSecurityContextAuthenticationAnnotation (), null , null , null );
102
+ Authentication auth1 = (Authentication )
103
+ this .resolver .resolveArgument (showSecurityContextAuthenticationAnnotation (), null , null , null );
102
104
assertThat (auth1 .getPrincipal ()).isEqualTo (principal );
103
105
}
104
106
@@ -109,87 +111,90 @@ public void resolveArgumentWithNullAuthentication() {
109
111
context .setAuthentication (null );
110
112
assertThatExceptionOfType (SpelEvaluationException .class )
111
113
.isThrownBy (() -> {
112
- resolver .resolveArgument (showSecurityContextAuthenticationWithPrincipal (), null , null , null );
114
+ this . resolver .resolveArgument (showSecurityContextAuthenticationWithPrincipal (), null , null , null );
113
115
});
114
116
context .setAuthentication (authentication );
115
117
}
116
118
117
119
@ Test
118
- public void resolveArgumentWithOptionalPrincipal () throws Exception {
120
+ public void resolveArgumentWithOptionalPrincipal () {
119
121
SecurityContext context = SecurityContextHolder .getContext ();
120
122
Authentication authentication = context .getAuthentication ();
121
123
context .setAuthentication (null );
122
- Object principalResult = resolver .resolveArgument (showSecurityContextAuthenticationWithOptionalPrincipal (), null , null , null );
124
+ Object principalResult =
125
+ this .resolver .resolveArgument (showSecurityContextAuthenticationWithOptionalPrincipal (), null , null , null );
123
126
assertThat (principalResult ).isNull ();
124
127
context .setAuthentication (authentication );
125
128
}
126
129
127
130
@ Test
128
- public void resolveArgumentWithPrincipal () throws Exception {
131
+ public void resolveArgumentWithPrincipal () {
129
132
String principal = "smith" ;
130
133
setAuthenticationPrincipal (principal );
131
- String principalResult = (String ) resolver .resolveArgument (showSecurityContextAuthenticationWithPrincipal (), null , null , null );
134
+ String principalResult = (String )
135
+ this .resolver .resolveArgument (showSecurityContextAuthenticationWithPrincipal (), null , null , null );
132
136
assertThat (principalResult ).isEqualTo (principal );
133
137
}
134
138
135
139
@ Test
136
- public void resolveArgumentUserDetails () throws Exception {
140
+ public void resolveArgumentUserDetails () {
137
141
setAuthenticationDetail (new User ("my_user" , "my_password" ,
138
142
AuthorityUtils .createAuthorityList ("ROLE_USER" )));
139
143
140
- User u = (User ) resolver .resolveArgument (showSecurityContextWithUserDetail (), null , null ,
144
+ User u = (User ) this . resolver .resolveArgument (showSecurityContextWithUserDetail (), null , null ,
141
145
null );
142
146
assertThat (u .getUsername ()).isEqualTo ("my_user" );
143
147
}
144
148
145
149
@ Test
146
- public void resolveArgumentSecurityContextErrorOnInvalidTypeImplicit () throws Exception {
150
+ public void resolveArgumentSecurityContextErrorOnInvalidTypeImplicit () {
147
151
String principal = "invalid_type_implicit" ;
148
152
setAuthenticationPrincipal (principal );
149
- assertThat (resolver .resolveArgument (showSecurityContextErrorOnInvalidTypeImplicit (), null , null , null ))
153
+ assertThat (this . resolver .resolveArgument (showSecurityContextErrorOnInvalidTypeImplicit (), null , null , null ))
150
154
.isNull ();
151
155
}
152
156
153
157
@ Test
154
- public void resolveArgumentSecurityContextErrorOnInvalidTypeFalse () throws Exception {
158
+ public void resolveArgumentSecurityContextErrorOnInvalidTypeFalse () {
155
159
String principal = "invalid_type_false" ;
156
160
setAuthenticationPrincipal (principal );
157
- assertThat (resolver .resolveArgument (showSecurityContextErrorOnInvalidTypeFalse (), null , null , null ))
161
+ assertThat (this . resolver .resolveArgument (showSecurityContextErrorOnInvalidTypeFalse (), null , null , null ))
158
162
.isNull ();
159
163
}
160
164
161
165
@ Test
162
166
public void resolveArgumentSecurityContextErrorOnInvalidTypeTrue () {
163
167
String principal = "invalid_type_true" ;
164
168
setAuthenticationPrincipal (principal );
165
- assertThatExceptionOfType (ClassCastException .class ).isThrownBy (() -> resolver . resolveArgument ( showSecurityContextErrorOnInvalidTypeTrue (), null ,
166
- null , null ));
169
+ assertThatExceptionOfType (ClassCastException .class ).isThrownBy (() ->
170
+ this . resolver . resolveArgument ( showSecurityContextErrorOnInvalidTypeTrue (), null , null , null ));
167
171
}
168
172
169
173
@ Test
170
- public void metaAnnotationWhenCurrentCustomSecurityContextThenInjectSecurityContext () throws Exception {
171
- assertThat (resolver .resolveArgument (showCurrentCustomSecurityContext (), null , null , null ))
174
+ public void metaAnnotationWhenCurrentCustomSecurityContextThenInjectSecurityContext () {
175
+ assertThat (this . resolver .resolveArgument (showCurrentCustomSecurityContext (), null , null , null ))
172
176
.isNotNull ();
173
177
}
174
178
175
179
@ Test
176
- public void metaAnnotationWhenCurrentAuthenticationThenInjectAuthentication () throws Exception {
180
+ public void metaAnnotationWhenCurrentAuthenticationThenInjectAuthentication () {
177
181
String principal = "current_authentcation" ;
178
182
setAuthenticationPrincipal (principal );
179
- Authentication auth1 = (Authentication ) resolver .resolveArgument (showCurrentAuthentication (), null , null , null );
183
+ Authentication auth1 = (Authentication )
184
+ this .resolver .resolveArgument (showCurrentAuthentication (), null , null , null );
180
185
assertThat (auth1 .getPrincipal ()).isEqualTo (principal );
181
186
}
182
187
183
188
@ Test
184
- public void metaAnnotationWhenCurrentSecurityWithErrorOnInvalidTypeThenInjectSecurityContext () throws Exception {
185
- assertThat (resolver .resolveArgument (showCurrentSecurityWithErrorOnInvalidType (), null , null , null ))
189
+ public void metaAnnotationWhenCurrentSecurityWithErrorOnInvalidTypeThenInjectSecurityContext () {
190
+ assertThat (this . resolver .resolveArgument (showCurrentSecurityWithErrorOnInvalidType (), null , null , null ))
186
191
.isNotNull ();
187
192
}
188
193
189
194
@ Test
190
195
public void metaAnnotationWhenCurrentSecurityWithErrorOnInvalidTypeThenMisMatch () {
191
- assertThatExceptionOfType (ClassCastException .class ).isThrownBy (() -> resolver . resolveArgument ( showCurrentSecurityWithErrorOnInvalidTypeMisMatch (), null ,
192
- null , null ));
196
+ assertThatExceptionOfType (ClassCastException .class ).isThrownBy (() ->
197
+ this . resolver . resolveArgument ( showCurrentSecurityWithErrorOnInvalidTypeMisMatch (), null , null , null ));
193
198
}
194
199
195
200
private MethodParameter showSecurityContextNoAnnotation () {
@@ -342,13 +347,13 @@ public void setAuthentication(Authentication authentication) {
342
347
@ Target ({ ElementType .PARAMETER })
343
348
@ Retention (RetentionPolicy .RUNTIME )
344
349
@ CurrentSecurityContext
345
- static @interface CurrentCustomSecurityContext {
350
+ @interface CurrentCustomSecurityContext {
346
351
}
347
352
348
353
@ Target ({ ElementType .PARAMETER })
349
354
@ Retention (RetentionPolicy .RUNTIME )
350
355
@ CurrentSecurityContext (expression = "authentication" )
351
- static @interface CurrentAuthentication {
356
+ @interface CurrentAuthentication {
352
357
}
353
358
354
359
@ Target ({ ElementType .PARAMETER })
0 commit comments