forked from spring-projects/spring-security
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes spring-projectsgh-14596
- Loading branch information
Showing
16 changed files
with
1,360 additions
and
27 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
...work/security/config/annotation/method/configuration/AuthorizationProxyConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2002-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.security.config.annotation.method.configuration; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.springframework.aop.framework.AopInfrastructureBean; | ||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.beans.factory.config.BeanDefinition; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Role; | ||
import org.springframework.core.annotation.AnnotationAwareOrderComparator; | ||
import org.springframework.security.authorization.AuthorizationAdvisorProxyFactory; | ||
import org.springframework.security.authorization.method.AuthorizationAdvisor; | ||
|
||
@Configuration(proxyBeanMethods = false) | ||
final class AuthorizationProxyConfiguration implements AopInfrastructureBean { | ||
|
||
@Bean | ||
@Role(BeanDefinition.ROLE_INFRASTRUCTURE) | ||
static AuthorizationAdvisorProxyFactory authorizationProxyFactory(ObjectProvider<AuthorizationAdvisor> provider) { | ||
List<AuthorizationAdvisor> advisors = new ArrayList<>(); | ||
provider.forEach(advisors::add); | ||
AnnotationAwareOrderComparator.sort(advisors); | ||
return new AuthorizationAdvisorProxyFactory(advisors); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...security/config/annotation/method/configuration/AuthorizationProxyConfigurationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright 2002-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.security.config.annotation.method.configuration; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.security.access.prepost.PostAuthorize; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.security.authorization.AuthorizationProxyFactory; | ||
import org.springframework.security.config.test.SpringTestContext; | ||
import org.springframework.security.config.test.SpringTestContextExtension; | ||
import org.springframework.security.test.context.annotation.SecurityTestExecutionListeners; | ||
import org.springframework.security.test.context.support.WithMockUser; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
|
||
/** | ||
* Tests for {@link PrePostMethodSecurityConfiguration}. | ||
* | ||
* @author Evgeniy Cheban | ||
* @author Josh Cummings | ||
*/ | ||
@ExtendWith({ SpringExtension.class, SpringTestContextExtension.class }) | ||
@SecurityTestExecutionListeners | ||
public class AuthorizationProxyConfigurationTests { | ||
|
||
public final SpringTestContext spring = new SpringTestContext(this); | ||
|
||
@Autowired | ||
AuthorizationProxyFactory proxyFactory; | ||
|
||
@WithMockUser | ||
@Test | ||
public void proxyWhenNotPreAuthorizedThenDenies() { | ||
this.spring.register(DefaultsConfig.class).autowire(); | ||
Toaster toaster = (Toaster) this.proxyFactory.proxy(new Toaster()); | ||
assertThatExceptionOfType(AccessDeniedException.class).isThrownBy(toaster::makeToast) | ||
.withMessage("Access Denied"); | ||
assertThatExceptionOfType(AccessDeniedException.class).isThrownBy(toaster::extractBread) | ||
.withMessage("Access Denied"); | ||
} | ||
|
||
@WithMockUser(roles = "ADMIN") | ||
@Test | ||
public void proxyWhenPreAuthorizedThenAllows() { | ||
this.spring.register(DefaultsConfig.class).autowire(); | ||
Toaster toaster = (Toaster) this.proxyFactory.proxy(new Toaster()); | ||
toaster.makeToast(); | ||
assertThat(toaster.extractBread()).isEqualTo("yummy"); | ||
} | ||
|
||
@EnableMethodSecurity | ||
@Configuration | ||
static class DefaultsConfig { | ||
|
||
} | ||
|
||
static class Toaster { | ||
|
||
@PreAuthorize("hasRole('ADMIN')") | ||
void makeToast() { | ||
|
||
} | ||
|
||
@PostAuthorize("hasRole('ADMIN')") | ||
String extractBread() { | ||
return "yummy"; | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.