Skip to content

Commit 5f90523

Browse files
committed
Polish CurrentSecurityContextArgumentResolvers
Fixes gh-7487
1 parent 5ef6e7e commit 5f90523

File tree

5 files changed

+93
-106
lines changed

5 files changed

+93
-106
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/configuration/WebMvcSecurityConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.springframework.context.annotation.Bean;
2222
import org.springframework.context.expression.BeanFactoryResolver;
2323
import org.springframework.expression.BeanResolver;
24-
import org.springframework.security.web.bind.support.CurrentSecurityContextArgumentResolver;
24+
import org.springframework.security.web.method.annotation.CurrentSecurityContextArgumentResolver;
2525
import org.springframework.security.web.method.annotation.AuthenticationPrincipalArgumentResolver;
2626
import org.springframework.security.web.method.annotation.CsrfTokenArgumentResolver;
2727
import org.springframework.security.web.servlet.support.csrf.CsrfRequestDataValueProcessor;
+13-25
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.security.web.bind.support;
16+
package org.springframework.security.web.method.annotation;
1717

1818
import java.lang.annotation.Annotation;
1919

@@ -62,10 +62,10 @@
6262
* </pre>
6363
*
6464
* <p>
65-
* Will resolve the SecurityContext argument using {@link SecurityContextHolder#getContext()} from
66-
* the {@link SecurityContextHolder}. If the {@link SecurityContext} is null, it will return null.
67-
* If the types do not match, null will be returned unless
68-
* {@link CurrentSecurityContext#errorOnInvalidType()} is true in which case a
65+
* Will resolve the {@link SecurityContext} argument using {@link SecurityContextHolder#getContext()} from
66+
* the {@link SecurityContextHolder}. If the {@link SecurityContext} is {@code null}, it will return {@code null}.
67+
* If the types do not match, {@code null} will be returned unless
68+
* {@link CurrentSecurityContext#errorOnInvalidType()} is {@code true} in which case a
6969
* {@link ClassCastException} will be thrown.
7070
* </p>
7171
*
@@ -78,32 +78,19 @@ public final class CurrentSecurityContextArgumentResolver
7878
private ExpressionParser parser = new SpelExpressionParser();
7979

8080
private BeanResolver beanResolver;
81+
8182
/**
82-
* check if this argument resolve can support the parameter.
83-
* @param parameter the method parameter.
84-
* @return true = it can support parameter.
85-
*
86-
* @see
87-
* org.springframework.web.method.support.HandlerMethodArgumentResolver#
88-
* supportsParameter(org.springframework.core.MethodParameter)
83+
* {@inheritDoc}
8984
*/
85+
@Override
9086
public boolean supportsParameter(MethodParameter parameter) {
9187
return findMethodAnnotation(CurrentSecurityContext.class, parameter) != null;
9288
}
9389

9490
/**
95-
* resolve the argument to inject into the controller parameter.
96-
* @param parameter the method parameter.
97-
* @param mavContainer the model and view container.
98-
* @param webRequest the web request.
99-
* @param binderFactory the web data binder factory.
100-
*
101-
* @see org.springframework.web.method.support.HandlerMethodArgumentResolver#
102-
* resolveArgument (org.springframework.core.MethodParameter,
103-
* org.springframework.web.method.support.ModelAndViewContainer,
104-
* org.springframework.web.context.request.NativeWebRequest,
105-
* org.springframework.web.bind.support.WebDataBinderFactory)
91+
* {@inheritDoc}
10692
*/
93+
@Override
10794
public Object resolveArgument(MethodParameter parameter,
10895
ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
10996
WebDataBinderFactory binderFactory) {
@@ -138,8 +125,9 @@ public Object resolveArgument(MethodParameter parameter,
138125
}
139126
return securityContextResult;
140127
}
128+
141129
/**
142-
* Sets the {@link BeanResolver} to be used on the expressions
130+
* Set the {@link BeanResolver} to be used on the expressions
143131
* @param beanResolver the {@link BeanResolver} to use
144132
*/
145133
public void setBeanResolver(BeanResolver beanResolver) {
@@ -148,7 +136,7 @@ public void setBeanResolver(BeanResolver beanResolver) {
148136
}
149137

150138
/**
151-
* Obtains the specified {@link Annotation} on the specified {@link MethodParameter}.
139+
* Obtain the specified {@link Annotation} on the specified {@link MethodParameter}.
152140
*
153141
* @param annotationClass the class of the {@link Annotation} to find on the
154142
* {@link MethodParameter}

web/src/main/java/org/springframework/security/web/reactive/result/method/annotation/CurrentSecurityContextArgumentResolver.java

+8-16
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
*/
1616
package org.springframework.security.web.reactive.result.method.annotation;
1717

18+
import java.lang.annotation.Annotation;
19+
1820
import org.reactivestreams.Publisher;
21+
import reactor.core.publisher.Mono;
22+
1923
import org.springframework.core.MethodParameter;
2024
import org.springframework.core.ReactiveAdapter;
2125
import org.springframework.core.ReactiveAdapterRegistry;
@@ -34,12 +38,10 @@
3438
import org.springframework.web.reactive.BindingContext;
3539
import org.springframework.web.reactive.result.method.HandlerMethodArgumentResolverSupport;
3640
import org.springframework.web.server.ServerWebExchange;
37-
import reactor.core.publisher.Mono;
38-
39-
import java.lang.annotation.Annotation;
4041

4142
/**
42-
* Resolves the SecurityContext
43+
* Resolves the {@link SecurityContext}
44+
*
4345
* @author Dan Zheng
4446
* @since 5.2
4547
*/
@@ -63,25 +65,15 @@ public void setBeanResolver(BeanResolver beanResolver) {
6365
}
6466

6567
/**
66-
* check if this argument resolve can support the parameter.
67-
* @param parameter the method parameter.
68-
* @return true = it can support parameter.
69-
*
70-
* @see
71-
* org.springframework.web.reactive.result.method.HandlerMethodArgumentResolver#
72-
* supportsParameter(org.springframework.core.MethodParameter)
68+
* {@inheritDoc}
7369
*/
7470
@Override
7571
public boolean supportsParameter(MethodParameter parameter) {
7672
return findMethodAnnotation(CurrentSecurityContext.class, parameter) != null;
7773
}
7874

7975
/**
80-
* resolve the argument to inject into the controller parameter.
81-
* @param parameter the method parameter.
82-
* @param bindingContext the binding context.
83-
* @param exchange the server web exchange.
84-
* @return the reactive mono object result.
76+
* {@inheritDoc}
8577
*/
8678
@Override
8779
public Mono<Object> resolveArgument(MethodParameter parameter, BindingContext bindingContext,
+41-36
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.security.web.bind.support;
16+
package org.springframework.security.web.method.annotation;
1717

1818
import java.lang.annotation.ElementType;
1919
import java.lang.annotation.Retention;
@@ -45,12 +45,11 @@
4545
*
4646
*/
4747
public class CurrentSecurityContextArgumentResolverTests {
48-
private Object expectedPrincipal;
4948
private CurrentSecurityContextArgumentResolver resolver;
5049

5150
@Before
5251
public void setup() {
53-
resolver = new CurrentSecurityContextArgumentResolver();
52+
this.resolver = new CurrentSecurityContextArgumentResolver();
5453
}
5554

5655
@After
@@ -60,45 +59,48 @@ public void cleanup() {
6059

6160
@Test
6261
public void supportsParameterNoAnnotation() {
63-
assertThat(resolver.supportsParameter(showSecurityContextNoAnnotation())).isFalse();
62+
assertThat(this.resolver.supportsParameter(showSecurityContextNoAnnotation())).isFalse();
6463
}
6564

6665
@Test
6766
public void supportsParameterAnnotation() {
68-
assertThat(resolver.supportsParameter(showSecurityContextAnnotation())).isTrue();
67+
assertThat(this.resolver.supportsParameter(showSecurityContextAnnotation())).isTrue();
6968
}
7069

7170
@Test
72-
public void resolveArgumentWithCustomSecurityContext() throws Exception {
71+
public void resolveArgumentWithCustomSecurityContext() {
7372
String principal = "custom_security_context";
7473
setAuthenticationPrincipalWithCustomSecurityContext(principal);
75-
CustomSecurityContext customSecurityContext = (CustomSecurityContext) resolver.resolveArgument(showAnnotationWithCustomSecurityContext(), null, null, null);
74+
CustomSecurityContext customSecurityContext = (CustomSecurityContext)
75+
this.resolver.resolveArgument(showAnnotationWithCustomSecurityContext(), null, null, null);
7676
assertThat(customSecurityContext.getAuthentication().getPrincipal()).isEqualTo(principal);
7777
}
7878

7979
@Test
80-
public void resolveArgumentWithCustomSecurityContextTypeMatch() throws Exception {
80+
public void resolveArgumentWithCustomSecurityContextTypeMatch() {
8181
String principal = "custom_security_context_type_match";
8282
setAuthenticationPrincipalWithCustomSecurityContext(principal);
83-
CustomSecurityContext customSecurityContext = (CustomSecurityContext) resolver.resolveArgument(showAnnotationWithCustomSecurityContext(), null, null, null);
83+
CustomSecurityContext customSecurityContext = (CustomSecurityContext)
84+
this.resolver.resolveArgument(showAnnotationWithCustomSecurityContext(), null, null, null);
8485
assertThat(customSecurityContext.getAuthentication().getPrincipal()).isEqualTo(principal);
8586
}
8687

8788
@Test
88-
public void resolveArgumentNullAuthentication() throws Exception {
89+
public void resolveArgumentNullAuthentication() {
8990
SecurityContext context = SecurityContextHolder.getContext();
9091
Authentication authentication = context.getAuthentication();
9192
context.setAuthentication(null);
92-
assertThat(resolver.resolveArgument(showSecurityContextAuthenticationAnnotation(), null, null, null))
93+
assertThat(this.resolver.resolveArgument(showSecurityContextAuthenticationAnnotation(), null, null, null))
9394
.isNull();
9495
context.setAuthentication(authentication);
9596
}
9697

9798
@Test
98-
public void resolveArgumentWithAuthentication() throws Exception {
99+
public void resolveArgumentWithAuthentication() {
99100
String principal = "john";
100101
setAuthenticationPrincipal(principal);
101-
Authentication auth1 = (Authentication) resolver.resolveArgument(showSecurityContextAuthenticationAnnotation(), null, null, null);
102+
Authentication auth1 = (Authentication)
103+
this.resolver.resolveArgument(showSecurityContextAuthenticationAnnotation(), null, null, null);
102104
assertThat(auth1.getPrincipal()).isEqualTo(principal);
103105
}
104106

@@ -109,87 +111,90 @@ public void resolveArgumentWithNullAuthentication() {
109111
context.setAuthentication(null);
110112
assertThatExceptionOfType(SpelEvaluationException.class)
111113
.isThrownBy(() -> {
112-
resolver.resolveArgument(showSecurityContextAuthenticationWithPrincipal(), null, null, null);
114+
this.resolver.resolveArgument(showSecurityContextAuthenticationWithPrincipal(), null, null, null);
113115
});
114116
context.setAuthentication(authentication);
115117
}
116118

117119
@Test
118-
public void resolveArgumentWithOptionalPrincipal() throws Exception {
120+
public void resolveArgumentWithOptionalPrincipal() {
119121
SecurityContext context = SecurityContextHolder.getContext();
120122
Authentication authentication = context.getAuthentication();
121123
context.setAuthentication(null);
122-
Object principalResult = resolver.resolveArgument(showSecurityContextAuthenticationWithOptionalPrincipal(), null, null, null);
124+
Object principalResult =
125+
this.resolver.resolveArgument(showSecurityContextAuthenticationWithOptionalPrincipal(), null, null, null);
123126
assertThat(principalResult).isNull();
124127
context.setAuthentication(authentication);
125128
}
126129

127130
@Test
128-
public void resolveArgumentWithPrincipal() throws Exception {
131+
public void resolveArgumentWithPrincipal() {
129132
String principal = "smith";
130133
setAuthenticationPrincipal(principal);
131-
String principalResult = (String) resolver.resolveArgument(showSecurityContextAuthenticationWithPrincipal(), null, null, null);
134+
String principalResult = (String)
135+
this.resolver.resolveArgument(showSecurityContextAuthenticationWithPrincipal(), null, null, null);
132136
assertThat(principalResult).isEqualTo(principal);
133137
}
134138

135139
@Test
136-
public void resolveArgumentUserDetails() throws Exception {
140+
public void resolveArgumentUserDetails() {
137141
setAuthenticationDetail(new User("my_user", "my_password",
138142
AuthorityUtils.createAuthorityList("ROLE_USER")));
139143

140-
User u = (User) resolver.resolveArgument(showSecurityContextWithUserDetail(), null, null,
144+
User u = (User) this.resolver.resolveArgument(showSecurityContextWithUserDetail(), null, null,
141145
null);
142146
assertThat(u.getUsername()).isEqualTo("my_user");
143147
}
144148

145149
@Test
146-
public void resolveArgumentSecurityContextErrorOnInvalidTypeImplicit() throws Exception {
150+
public void resolveArgumentSecurityContextErrorOnInvalidTypeImplicit() {
147151
String principal = "invalid_type_implicit";
148152
setAuthenticationPrincipal(principal);
149-
assertThat(resolver.resolveArgument(showSecurityContextErrorOnInvalidTypeImplicit(), null, null, null))
153+
assertThat(this.resolver.resolveArgument(showSecurityContextErrorOnInvalidTypeImplicit(), null, null, null))
150154
.isNull();
151155
}
152156

153157
@Test
154-
public void resolveArgumentSecurityContextErrorOnInvalidTypeFalse() throws Exception {
158+
public void resolveArgumentSecurityContextErrorOnInvalidTypeFalse() {
155159
String principal = "invalid_type_false";
156160
setAuthenticationPrincipal(principal);
157-
assertThat(resolver.resolveArgument(showSecurityContextErrorOnInvalidTypeFalse(), null, null, null))
161+
assertThat(this.resolver.resolveArgument(showSecurityContextErrorOnInvalidTypeFalse(), null, null, null))
158162
.isNull();
159163
}
160164

161165
@Test
162166
public void resolveArgumentSecurityContextErrorOnInvalidTypeTrue() {
163167
String principal = "invalid_type_true";
164168
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));
167171
}
168172

169173
@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))
172176
.isNotNull();
173177
}
174178

175179
@Test
176-
public void metaAnnotationWhenCurrentAuthenticationThenInjectAuthentication() throws Exception {
180+
public void metaAnnotationWhenCurrentAuthenticationThenInjectAuthentication() {
177181
String principal = "current_authentcation";
178182
setAuthenticationPrincipal(principal);
179-
Authentication auth1 = (Authentication) resolver.resolveArgument(showCurrentAuthentication(), null, null, null);
183+
Authentication auth1 = (Authentication)
184+
this.resolver.resolveArgument(showCurrentAuthentication(), null, null, null);
180185
assertThat(auth1.getPrincipal()).isEqualTo(principal);
181186
}
182187

183188
@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))
186191
.isNotNull();
187192
}
188193

189194
@Test
190195
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));
193198
}
194199

195200
private MethodParameter showSecurityContextNoAnnotation() {
@@ -342,13 +347,13 @@ public void setAuthentication(Authentication authentication) {
342347
@Target({ ElementType.PARAMETER })
343348
@Retention(RetentionPolicy.RUNTIME)
344349
@CurrentSecurityContext
345-
static @interface CurrentCustomSecurityContext {
350+
@interface CurrentCustomSecurityContext {
346351
}
347352

348353
@Target({ ElementType.PARAMETER })
349354
@Retention(RetentionPolicy.RUNTIME)
350355
@CurrentSecurityContext(expression = "authentication")
351-
static @interface CurrentAuthentication {
356+
@interface CurrentAuthentication {
352357
}
353358

354359
@Target({ ElementType.PARAMETER })

0 commit comments

Comments
 (0)