Skip to content

Commit 147ab42

Browse files
committed
Revert "Pick up AuthorizationManager Bean"
This reverts commit 32b83aa. Issue gh-11067
1 parent 39b0620 commit 147ab42

File tree

4 files changed

+5
-145
lines changed

4 files changed

+5
-145
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/configurers/AuthorizeHttpRequestsConfigurer.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import javax.servlet.http.HttpServletRequest;
2222

2323
import org.springframework.context.ApplicationContext;
24-
import org.springframework.core.ParameterizedTypeReference;
25-
import org.springframework.core.ResolvableType;
2624
import org.springframework.http.HttpMethod;
2725
import org.springframework.security.authorization.AuthenticatedAuthorizationManager;
2826
import org.springframework.security.authorization.AuthorityAuthorizationManager;
@@ -54,10 +52,6 @@ public final class AuthorizeHttpRequestsConfigurer<H extends HttpSecurityBuilder
5452
static final AuthorizationManager<RequestAuthorizationContext> permitAllAuthorizationManager = (a,
5553
o) -> new AuthorizationDecision(true);
5654

57-
static final ResolvableType REQUEST_AUTHORIZATION_MANAGER_TYPE = ResolvableType
58-
.forType(new ParameterizedTypeReference<AuthorizationManager<HttpServletRequest>>() {
59-
});
60-
6155
private final AuthorizationManagerRequestMatcherRegistry registry;
6256

6357
private final AuthorizationEventPublisher publisher;
@@ -143,15 +137,9 @@ private AuthorizationManager<HttpServletRequest> createAuthorizationManager() {
143137
Assert.state(this.unmappedMatchers == null,
144138
() -> "An incomplete mapping was found for " + this.unmappedMatchers
145139
+ ". Try completing it with something like requestUrls().<something>.hasRole('USER')");
146-
if (this.mappingCount > 0) {
147-
return postProcess(this.managerBuilder.build());
148-
}
149-
if (this.getApplicationContext().getBeanNamesForType(REQUEST_AUTHORIZATION_MANAGER_TYPE).length > 0) {
150-
return (AuthorizationManager<HttpServletRequest>) this.getApplicationContext()
151-
.getBeanProvider(REQUEST_AUTHORIZATION_MANAGER_TYPE).getObject();
152-
}
153-
throw new IllegalStateException(
140+
Assert.state(this.mappingCount > 0,
154141
"At least one mapping is required (for example, authorizeHttpRequests().anyRequest().authenticated())");
142+
return postProcess(this.managerBuilder.build());
155143
}
156144

157145
@Override

config/src/main/java/org/springframework/security/config/web/server/ServerHttpSecurity.java

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -37,7 +37,6 @@
3737
import org.springframework.beans.BeansException;
3838
import org.springframework.context.ApplicationContext;
3939
import org.springframework.core.Ordered;
40-
import org.springframework.core.ParameterizedTypeReference;
4140
import org.springframework.core.ResolvableType;
4241
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
4342
import org.springframework.core.convert.converter.Converter;
@@ -256,10 +255,6 @@
256255
*/
257256
public class ServerHttpSecurity {
258257

259-
private static final ResolvableType REQUEST_AUTHORIZATION_MANAGER_TYPE = ResolvableType
260-
.forType(new ParameterizedTypeReference<ReactiveAuthorizationManager<ServerWebExchange>>() {
261-
});
262-
263258
private ServerWebExchangeMatcher securityMatcher = ServerWebExchangeMatchers.anyExchange();
264259

265260
private AuthorizeExchangeSpec authorizeExchange;
@@ -1589,8 +1584,6 @@ public class AuthorizeExchangeSpec extends AbstractServerWebExchangeMatcherRegis
15891584

15901585
private boolean anyExchangeRegistered;
15911586

1592-
private boolean mappingRegistered;
1593-
15941587
/**
15951588
* Allows method chaining to continue configuring the {@link ServerHttpSecurity}
15961589
* @return the {@link ServerHttpSecurity} to continue configuring
@@ -1623,23 +1616,10 @@ protected Access registerMatcher(ServerWebExchangeMatcher matcher) {
16231616
protected void configure(ServerHttpSecurity http) {
16241617
Assert.state(this.matcher == null,
16251618
() -> "The matcher " + this.matcher + " does not have an access rule defined");
1626-
AuthorizationWebFilter result = new AuthorizationWebFilter(authorizationManager());
1619+
AuthorizationWebFilter result = new AuthorizationWebFilter(this.managerBldr.build());
16271620
http.addFilterAt(result, SecurityWebFiltersOrder.AUTHORIZATION);
16281621
}
16291622

1630-
private ReactiveAuthorizationManager<ServerWebExchange> authorizationManager() {
1631-
if (this.mappingRegistered) {
1632-
return this.managerBldr.build();
1633-
}
1634-
ReactiveAuthorizationManager<ServerWebExchange> anyExchange = getBeanOrNull(
1635-
REQUEST_AUTHORIZATION_MANAGER_TYPE);
1636-
if (anyExchange != null) {
1637-
return anyExchange;
1638-
}
1639-
throw new IllegalStateException(
1640-
"At least one mapping is required (for example, authorizeExchange().anyExchange().authenticated())");
1641-
}
1642-
16431623
/**
16441624
* Configures the access for a particular set of exchanges.
16451625
*/
@@ -1730,7 +1710,6 @@ public AuthorizeExchangeSpec access(ReactiveAuthorizationManager<AuthorizationCo
17301710
AuthorizeExchangeSpec.this.managerBldr
17311711
.add(new ServerWebExchangeMatcherEntry<>(AuthorizeExchangeSpec.this.matcher, manager));
17321712
AuthorizeExchangeSpec.this.matcher = null;
1733-
AuthorizeExchangeSpec.this.mappingRegistered = true;
17341713
return AuthorizeExchangeSpec.this;
17351714
}
17361715

config/src/test/java/org/springframework/security/config/annotation/web/configurers/AuthorizeHttpRequestsConfigurerTests.java

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import javax.servlet.http.HttpServletRequest;
2222

23-
import org.aopalliance.intercept.MethodInvocation;
2423
import org.junit.jupiter.api.Test;
2524
import org.junit.jupiter.api.extension.ExtendWith;
2625

@@ -50,12 +49,10 @@
5049
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
5150

5251
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
53-
import static org.mockito.BDDMockito.given;
5452
import static org.mockito.Mockito.any;
5553
import static org.mockito.Mockito.mock;
5654
import static org.mockito.Mockito.spy;
5755
import static org.mockito.Mockito.verify;
58-
import static org.mockito.Mockito.verifyNoInteractions;
5956
import static org.springframework.security.config.Customizer.withDefaults;
6057
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
6158
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
@@ -399,20 +396,6 @@ public void getWhenAnyRequestAuthenticatedConfiguredAndUserLoggedInThenRespondsW
399396
this.mvc.perform(requestWithUser).andExpect(status().isOk());
400397
}
401398

402-
@Test
403-
public void getWhenOnlyAuthorizationManagerBeanThenRespondsWithOk() throws Exception {
404-
this.spring.register(NoRequestsConfig.class, AuthorizationManagerConfig.class, BasicController.class)
405-
.autowire();
406-
AuthorizationManager<HttpServletRequest> request = (AuthorizationManager<HttpServletRequest>) this.spring
407-
.getContext().getBean("request");
408-
given(request.check(any(), any())).willReturn(new AuthorizationDecision(true));
409-
this.mvc.perform(get("/")).andExpect(status().isOk());
410-
verify(request).check(any(), any());
411-
AuthorizationManager<MethodInvocation> method = (AuthorizationManager<MethodInvocation>) this.spring
412-
.getContext().getBean("method");
413-
verifyNoInteractions(method);
414-
}
415-
416399
@EnableWebSecurity
417400
static class NoRequestsConfig {
418401

@@ -743,25 +726,6 @@ AuthorizationEventPublisher authorizationEventPublisher() {
743726

744727
}
745728

746-
@Configuration
747-
static class AuthorizationManagerConfig {
748-
749-
private final AuthorizationManager<HttpServletRequest> request = mock(AuthorizationManager.class);
750-
751-
private final AuthorizationManager<MethodInvocation> method = mock(AuthorizationManager.class);
752-
753-
@Bean
754-
AuthorizationManager<HttpServletRequest> request() {
755-
return this.request;
756-
}
757-
758-
@Bean
759-
AuthorizationManager<MethodInvocation> method() {
760-
return this.method;
761-
}
762-
763-
}
764-
765729
@RestController
766730
static class BasicController {
767731

config/src/test/java/org/springframework/security/config/web/server/AuthorizeExchangeSpecTests.java

Lines changed: 1 addition & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -16,29 +16,14 @@
1616

1717
package org.springframework.security.config.web.server;
1818

19-
import org.aopalliance.intercept.MethodInvocation;
2019
import org.junit.jupiter.api.Test;
21-
import reactor.core.publisher.Mono;
2220

23-
import org.springframework.context.annotation.Bean;
24-
import org.springframework.context.annotation.Configuration;
2521
import org.springframework.http.HttpMethod;
26-
import org.springframework.security.authorization.ReactiveAuthorizationManager;
27-
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
2822
import org.springframework.security.config.annotation.web.reactive.ServerHttpSecurityConfigurationBuilder;
29-
import org.springframework.security.config.test.SpringTestContext;
3023
import org.springframework.security.test.web.reactive.server.WebTestClientBuilder;
31-
import org.springframework.security.web.server.SecurityWebFilterChain;
3224
import org.springframework.test.web.reactive.server.WebTestClient;
33-
import org.springframework.web.server.ServerWebExchange;
3425

3526
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
36-
import static org.mockito.ArgumentMatchers.any;
37-
import static org.mockito.BDDMockito.given;
38-
import static org.mockito.Mockito.mock;
39-
import static org.mockito.Mockito.verify;
40-
import static org.mockito.Mockito.verifyNoInteractions;
41-
import static org.springframework.security.config.Customizer.withDefaults;
4227

4328
/**
4429
* @author Rob Winch
@@ -48,8 +33,6 @@ public class AuthorizeExchangeSpecTests {
4833

4934
ServerHttpSecurity http = ServerHttpSecurityConfigurationBuilder.httpWithDefaultAuthentication();
5035

51-
public final SpringTestContext spring = new SpringTestContext(this);
52-
5336
@Test
5437
public void antMatchersWhenMethodAndPatternsThenDiscriminatesByMethod() {
5538
this.http.csrf().disable().authorizeExchange().pathMatchers(HttpMethod.POST, "/a", "/b").denyAll().anyExchange()
@@ -124,26 +107,6 @@ public void antMatchersWhenPatternsInLambdaThenAnyMethod() {
124107
// @formatter:on
125108
}
126109

127-
@Test
128-
public void buildWhenAuthorizationManagerThenWorks() {
129-
this.spring.register(NoRequestsConfig.class, AuthorizationManagerConfig.class).autowire();
130-
ReactiveAuthorizationManager<ServerWebExchange> request = (ReactiveAuthorizationManager<ServerWebExchange>) this.spring
131-
.getContext().getBean("request");
132-
given(request.verify(any(), any())).willReturn(Mono.empty());
133-
SecurityWebFilterChain filterChain = this.spring.getContext().getBean(SecurityWebFilterChain.class);
134-
WebTestClient client = WebTestClientBuilder.bindToWebFilters(filterChain).build();
135-
// @formatter:off
136-
client.get()
137-
.uri("/a")
138-
.exchange()
139-
.expectStatus().isOk();
140-
// @formatter:on
141-
verify(request).verify(any(), any());
142-
ReactiveAuthorizationManager<MethodInvocation> method = (ReactiveAuthorizationManager<MethodInvocation>) this.spring
143-
.getContext().getBean("method");
144-
verifyNoInteractions(method);
145-
}
146-
147110
@Test
148111
public void antMatchersWhenNoAccessAndAnotherMatcherThenThrowsException() {
149112
this.http.authorizeExchange().pathMatchers("/incomplete");
@@ -178,38 +141,4 @@ private WebTestClient buildClient() {
178141
return WebTestClientBuilder.bindToWebFilters(this.http.build()).build();
179142
}
180143

181-
@EnableWebFluxSecurity
182-
static class NoRequestsConfig {
183-
184-
@Bean
185-
SecurityWebFilterChain filterChain(ServerHttpSecurity http) {
186-
// @formatter:off
187-
return http
188-
.authorizeExchange(withDefaults())
189-
.build();
190-
// @formatter:on
191-
}
192-
193-
}
194-
195-
@Configuration
196-
static class AuthorizationManagerConfig {
197-
198-
private final ReactiveAuthorizationManager<ServerWebExchange> request = mock(
199-
ReactiveAuthorizationManager.class);
200-
201-
private final ReactiveAuthorizationManager<MethodInvocation> method = mock(ReactiveAuthorizationManager.class);
202-
203-
@Bean
204-
ReactiveAuthorizationManager<ServerWebExchange> request() {
205-
return this.request;
206-
}
207-
208-
@Bean
209-
ReactiveAuthorizationManager<MethodInvocation> method() {
210-
return this.method;
211-
}
212-
213-
}
214-
215144
}

0 commit comments

Comments
 (0)