Skip to content

Commit f4eda99

Browse files
committed
Make AuthorizationProxyFactory#proxy Generic
Closes gh-16706
1 parent 226e81d commit f4eda99

File tree

6 files changed

+18
-16
lines changed

6 files changed

+18
-16
lines changed

config/src/main/java/org/springframework/security/config/annotation/method/configuration/AuthorizationProxyWebConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public Object visit(AuthorizationAdvisorProxyFactory proxyFactory, Object target
5151
if (target instanceof ModelAndView mav) {
5252
View view = mav.getView();
5353
String viewName = mav.getViewName();
54-
Map<String, Object> model = (Map<String, Object>) proxyFactory.proxy(mav.getModel());
54+
Map<String, Object> model = proxyFactory.proxy(mav.getModel());
5555
ModelAndView proxied = (view != null) ? new ModelAndView(view, model)
5656
: new ModelAndView(viewName, model);
5757
proxied.setStatus(mav.getStatus());

config/src/test/java/org/springframework/security/config/annotation/method/configuration/AuthorizationProxyConfigurationTests.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -58,7 +58,7 @@ public class AuthorizationProxyConfigurationTests {
5858
@Test
5959
public void proxyWhenNotPreAuthorizedThenDenies() {
6060
this.spring.register(DefaultsConfig.class).autowire();
61-
Toaster toaster = (Toaster) this.proxyFactory.proxy(new Toaster());
61+
Toaster toaster = this.proxyFactory.proxy(new Toaster());
6262
assertThatExceptionOfType(AccessDeniedException.class).isThrownBy(toaster::makeToast)
6363
.withMessage("Access Denied");
6464
assertThatExceptionOfType(AccessDeniedException.class).isThrownBy(toaster::extractBread)
@@ -69,15 +69,15 @@ public void proxyWhenNotPreAuthorizedThenDenies() {
6969
@Test
7070
public void proxyWhenPreAuthorizedThenAllows() {
7171
this.spring.register(DefaultsConfig.class).autowire();
72-
Toaster toaster = (Toaster) this.proxyFactory.proxy(new Toaster());
72+
Toaster toaster = this.proxyFactory.proxy(new Toaster());
7373
toaster.makeToast();
7474
assertThat(toaster.extractBread()).isEqualTo("yummy");
7575
}
7676

7777
@Test
7878
public void proxyReactiveWhenNotPreAuthorizedThenDenies() {
7979
this.spring.register(ReactiveDefaultsConfig.class).autowire();
80-
Toaster toaster = (Toaster) this.proxyFactory.proxy(new Toaster());
80+
Toaster toaster = this.proxyFactory.proxy(new Toaster());
8181
Authentication user = TestAuthentication.authenticatedUser();
8282
StepVerifier
8383
.create(toaster.reactiveMakeToast().contextWrite(ReactiveSecurityContextHolder.withAuthentication(user)))
@@ -90,7 +90,7 @@ public void proxyReactiveWhenNotPreAuthorizedThenDenies() {
9090
@Test
9191
public void proxyReactiveWhenPreAuthorizedThenAllows() {
9292
this.spring.register(ReactiveDefaultsConfig.class).autowire();
93-
Toaster toaster = (Toaster) this.proxyFactory.proxy(new Toaster());
93+
Toaster toaster = this.proxyFactory.proxy(new Toaster());
9494
Authentication admin = TestAuthentication.authenticatedAdmin();
9595
StepVerifier
9696
.create(toaster.reactiveMakeToast().contextWrite(ReactiveSecurityContextHolder.withAuthentication(admin)))

core/src/main/java/org/springframework/security/aot/hint/AuthorizeReturnObjectHintsRegistrar.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -109,7 +109,7 @@ public void registerHints(RuntimeHints hints, ConfigurableListableBeanFactory be
109109
}
110110

111111
private void registerProxy(RuntimeHints hints, Class<?> clazz) {
112-
Class<?> proxied = (Class<?>) this.proxyFactory.proxy(clazz);
112+
Class<?> proxied = this.proxyFactory.proxy(clazz);
113113
if (proxied == null) {
114114
return;
115115
}

core/src/main/java/org/springframework/security/authorization/AuthorizationProxyFactory.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
* A factory for wrapping arbitrary objects in authorization-related advice
2121
*
2222
* @author Josh Cummings
23+
* @author daewon kim
2324
* @since 6.3
2425
* @see org.springframework.security.authorization.method.AuthorizationAdvisorProxyFactory
2526
*/
@@ -30,11 +31,12 @@ public interface AuthorizationProxyFactory {
3031
*
3132
* <p>
3233
* Please check the implementation for which kinds of objects it supports.
34+
* @param <T> the type of the object being proxied
3335
* @param object the object to proxy
3436
* @return the proxied object
3537
* @throws org.springframework.aop.framework.AopConfigException if a proxy cannot be
3638
* created
3739
*/
38-
Object proxy(Object object);
40+
<T> T proxy(T object);
3941

4042
}

core/src/main/java/org/springframework/security/authorization/method/AuthorizationAdvisorProxyFactory.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,16 @@ public void afterSingletonsInstantiated() {
172172
* @return the proxied instance
173173
*/
174174
@Override
175-
public Object proxy(Object target) {
175+
public <T> T proxy(T target) {
176176
if (target == null) {
177177
return null;
178178
}
179179
if (target instanceof AuthorizationProxy proxied) {
180-
return proxied;
180+
return (T) proxied;
181181
}
182182
Object proxied = this.visitor.visit(this, target);
183183
if (proxied != null) {
184-
return proxied;
184+
return (T) proxied;
185185
}
186186
ProxyFactory factory = new ProxyFactory(target);
187187
factory.addAdvisors(this.authorizationProxy);
@@ -191,7 +191,7 @@ public Object proxy(Object target) {
191191
factory.addInterface(AuthorizationProxy.class);
192192
factory.setOpaque(true);
193193
factory.setProxyTargetClass(!Modifier.isFinal(target.getClass().getModifiers()));
194-
return factory.getProxy();
194+
return (T) factory.getProxy();
195195
}
196196

197197
/**
@@ -442,7 +442,7 @@ public Object visit(AuthorizationAdvisorProxyFactory proxyFactory, Object target
442442

443443
@SuppressWarnings("unchecked")
444444
private <T> T proxyCast(AuthorizationProxyFactory proxyFactory, T target) {
445-
return (T) proxyFactory.proxy(target);
445+
return proxyFactory.proxy(target);
446446
}
447447

448448
private <T> Iterable<T> proxyIterable(AuthorizationProxyFactory proxyFactory, Iterable<T> iterable) {

core/src/test/java/org/springframework/security/authorization/AuthorizationAdvisorProxyFactoryTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ public void setTargetVisitorThenUses() {
335335
@Test
336336
public void setTargetVisitorIgnoreValueTypesThenIgnores() {
337337
AuthorizationAdvisorProxyFactory factory = AuthorizationAdvisorProxyFactory.withDefaults();
338-
assertThatExceptionOfType(ClassCastException.class).isThrownBy(() -> ((Integer) factory.proxy(35)).intValue());
338+
assertThatExceptionOfType(ClassCastException.class).isThrownBy(() -> factory.proxy(35).intValue());
339339
factory.setTargetVisitor(TargetVisitor.defaultsSkipValueTypes());
340340
assertThat(factory.proxy(35)).isEqualTo(35);
341341
}

0 commit comments

Comments
 (0)