Skip to content

Commit b448954

Browse files
Introduce AuthorizationManagerWebInvocationPrivilegeEvaluator
Closes gh-10590
1 parent b0c7d77 commit b448954

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2002-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.web.access;
18+
19+
import javax.servlet.http.HttpServletRequest;
20+
21+
import org.springframework.security.authorization.AuthorizationDecision;
22+
import org.springframework.security.authorization.AuthorizationManager;
23+
import org.springframework.security.core.Authentication;
24+
import org.springframework.security.web.FilterInvocation;
25+
import org.springframework.util.Assert;
26+
27+
/**
28+
* An implementation of {@link WebInvocationPrivilegeEvaluator} which delegates the checks
29+
* to an instance of {@link AuthorizationManager}
30+
*
31+
* @author Marcus Da Coregio
32+
* @since 5.7
33+
*/
34+
public final class AuthorizationManagerWebInvocationPrivilegeEvaluator implements WebInvocationPrivilegeEvaluator {
35+
36+
private final AuthorizationManager<HttpServletRequest> authorizationManager;
37+
38+
public AuthorizationManagerWebInvocationPrivilegeEvaluator(
39+
AuthorizationManager<HttpServletRequest> authorizationManager) {
40+
Assert.notNull(authorizationManager, "authorizationManager cannot be null");
41+
this.authorizationManager = authorizationManager;
42+
}
43+
44+
@Override
45+
public boolean isAllowed(String uri, Authentication authentication) {
46+
return isAllowed(null, uri, null, authentication);
47+
}
48+
49+
@Override
50+
public boolean isAllowed(String contextPath, String uri, String method, Authentication authentication) {
51+
FilterInvocation filterInvocation = new FilterInvocation(contextPath, uri, method);
52+
AuthorizationDecision decision = this.authorizationManager.check(() -> authentication,
53+
filterInvocation.getHttpRequest());
54+
return decision != null && decision.isGranted();
55+
}
56+
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2002-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.web.access;
18+
19+
import javax.servlet.http.HttpServletRequest;
20+
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.mockito.InjectMocks;
24+
import org.mockito.Mock;
25+
import org.mockito.junit.MockitoJUnitRunner;
26+
27+
import org.springframework.security.authentication.TestAuthentication;
28+
import org.springframework.security.authorization.AuthorizationDecision;
29+
import org.springframework.security.authorization.AuthorizationManager;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
33+
import static org.mockito.ArgumentMatchers.any;
34+
import static org.mockito.BDDMockito.given;
35+
import static org.mockito.Mockito.verify;
36+
37+
@RunWith(MockitoJUnitRunner.class)
38+
public class AuthorizationManagerWebInvocationPrivilegeEvaluatorTests {
39+
40+
@InjectMocks
41+
private AuthorizationManagerWebInvocationPrivilegeEvaluator privilegeEvaluator;
42+
43+
@Mock
44+
private AuthorizationManager<HttpServletRequest> authorizationManager;
45+
46+
@Test
47+
public void constructorWhenAuthorizationManagerNullThenIllegalArgument() {
48+
assertThatIllegalArgumentException()
49+
.isThrownBy(() -> new AuthorizationManagerWebInvocationPrivilegeEvaluator(null))
50+
.withMessage("authorizationManager cannot be null");
51+
}
52+
53+
@Test
54+
public void isAllowedWhenAuthorizationManagerAllowsThenAllowedTrue() {
55+
given(this.authorizationManager.check(any(), any())).willReturn(new AuthorizationDecision(true));
56+
boolean allowed = this.privilegeEvaluator.isAllowed("/test", TestAuthentication.authenticatedUser());
57+
assertThat(allowed).isTrue();
58+
verify(this.authorizationManager).check(any(), any());
59+
}
60+
61+
@Test
62+
public void isAllowedWhenAuthorizationManagerDeniesAllowedFalse() {
63+
given(this.authorizationManager.check(any(), any())).willReturn(new AuthorizationDecision(false));
64+
boolean allowed = this.privilegeEvaluator.isAllowed("/test", TestAuthentication.authenticatedUser());
65+
assertThat(allowed).isFalse();
66+
}
67+
68+
}

0 commit comments

Comments
 (0)