Skip to content

Commit 9d14e8b

Browse files
Revert code
Signed-off-by: Tran Ngoc Nhan <ngocnhan.tran1996@gmail.com>
1 parent 8b2bf34 commit 9d14e8b

File tree

5 files changed

+61
-113
lines changed

5 files changed

+61
-113
lines changed

core/src/main/java/org/springframework/security/access/expression/SecurityExpressionRoot.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,21 +169,20 @@ public final boolean hasAllRoles(String... roles) {
169169

170170
@Override
171171
public final boolean hasScope(String scope) {
172-
return isGranted(this.authorizationManagerFactory.hasScope(scope));
172+
assertScope(scope);
173+
return isGranted(this.authorizationManagerFactory.hasAuthority(this.defaultScopePrefix + scope));
173174
}
174175

175176
@Override
176-
public boolean hasAnyScope(String... scopes) {
177+
public final boolean hasAnyScope(String... scopes) {
178+
Assert.notNull(scopes, "scopes cannot be null");
177179
if (this.authorizationManagerFactory instanceof DefaultAuthorizationManagerFactory<T>) {
178-
String scopePrefix = this.defaultScopePrefix;
179-
for (int index = 0; index < scopes.length; index++) {
180-
String scope = scopes[index];
181-
if (scope.startsWith(scopePrefix)) {
182-
scopes[index] = scope.substring(scopePrefix.length());
183-
}
180+
for (int i = 0; i < scopes.length; i++) {
181+
assertScope(scopes[i]);
182+
scopes[i] = this.defaultScopePrefix + scopes[i];
184183
}
185184
}
186-
return isGranted(this.authorizationManagerFactory.hasAnyScope(scopes));
185+
return isGranted(this.authorizationManagerFactory.hasAnyAuthority(scopes));
187186
}
188187

189188
@Override
@@ -326,4 +325,11 @@ public void setPermissionEvaluator(PermissionEvaluator permissionEvaluator) {
326325
this.permissionEvaluator = permissionEvaluator;
327326
}
328327

328+
private void assertScope(String scope) {
329+
Assert.notNull(scope, "scope cannot be null");
330+
Assert.isTrue(!scope.startsWith(this.defaultScopePrefix), () -> scope + " should not start with '"
331+
+ this.defaultScopePrefix + "' since '" + this.defaultScopePrefix
332+
+ "' is automatically prepended when using hasScope and hasAnyScope. Consider using AuthorityAuthorizationManager#hasAuthority or #hasAnyAuthority instead.");
333+
}
334+
329335
}

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

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -124,59 +124,6 @@ public static <T> AuthorityAuthorizationManager<T> hasAnyAuthority(String... aut
124124
return new AuthorityAuthorizationManager<>(authorities);
125125
}
126126

127-
/**
128-
* Create an {@link AuthorityAuthorizationManager} that requires an
129-
* {@link Authentication} to have a {@code SCOPE_scope} authority.
130-
*
131-
* <p>
132-
* For example, if you call {@code hasScope("read")}, then this will require that each
133-
* authentication have a {@link org.springframework.security.core.GrantedAuthority}
134-
* whose value is {@code SCOPE_read}.
135-
*
136-
* <p>
137-
* This would equivalent to calling
138-
* {@code AuthorityAuthorizationManager#hasAuthority("SCOPE_read")}.
139-
* @param scope the scope value to require
140-
* @param <T> the secure object
141-
* @return an {@link AuthorityAuthorizationManager} that requires a
142-
* {@code "SCOPE_scope"} authority
143-
*/
144-
public static <T> AuthorityAuthorizationManager<T> hasScope(String scope) {
145-
assertScope(scope);
146-
return hasAuthority("SCOPE_" + scope);
147-
}
148-
149-
/**
150-
* Create an {@link AuthorityAuthorizationManager} that requires an
151-
* {@link Authentication} to have at least one authority among {@code SCOPE_scope1},
152-
* {@code SCOPE_scope2}, ... {@code SCOPE_scopeN}.
153-
*
154-
* <p>
155-
* For example, if you call {@code hasAnyScope("read", "write")}, then this will
156-
* require that each authentication have at least a
157-
* {@link org.springframework.security.core.GrantedAuthority} whose value is either
158-
* {@code SCOPE_read} or {@code SCOPE_write}.
159-
*
160-
* <p>
161-
* This would equivalent to calling
162-
* {@code AuthorityAuthorizationManager#hasAnyAuthority("SCOPE_read", "SCOPE_write")}.
163-
* @param scopes the scope values to allow
164-
* @param <T> the secure object
165-
* @return an {@link AuthorityAuthorizationManager} that requires at least one
166-
* authority among {@code "SCOPE_scope1"}, {@code SCOPE_scope2}, ...
167-
* {@code SCOPE_scopeN}.
168-
*
169-
*/
170-
public static <T> AuthorityAuthorizationManager<T> hasAnyScope(String... scopes) {
171-
Assert.notNull(scopes, "scopes cannot be null");
172-
String[] mappedScopes = new String[scopes.length];
173-
for (int i = 0; i < scopes.length; i++) {
174-
assertScope(scopes[i]);
175-
mappedScopes[i] = "SCOPE_" + scopes[i];
176-
}
177-
return hasAnyAuthority(mappedScopes);
178-
}
179-
180127
private static String[] toNamedRolesArray(String rolePrefix, String[] roles) {
181128
String[] result = new String[roles.length];
182129
for (int i = 0; i < roles.length; i++) {
@@ -189,14 +136,6 @@ private static String[] toNamedRolesArray(String rolePrefix, String[] roles) {
189136
return result;
190137
}
191138

192-
private static void assertScope(String scope) {
193-
Assert.notNull(scope, "scope cannot be null");
194-
Assert.isTrue(!scope.startsWith("SCOPE_"),
195-
() -> scope + " should not start with SCOPE_ since SCOPE_"
196-
+ " is automatically prepended when using hasScope and hasAnyScope. Consider using "
197-
+ " AuthorityReactiveAuthorizationManager#hasAuthority or #hasAnyAuthority instead.");
198-
}
199-
200139
/**
201140
* {@inheritDoc}
202141
*/

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

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -109,28 +109,6 @@ default AuthorizationManager<T> hasAllAuthorities(String... authorities) {
109109
return AllAuthoritiesAuthorizationManager.hasAllAuthorities(authorities);
110110
}
111111

112-
/**
113-
* Creates an {@link AuthorizationManager} that requires users to have the specified
114-
* scope.
115-
* @param scope the scope (automatically prepended with SCOPE_) that should be
116-
* required to allow access (i.e. write, read, etc.)
117-
* @return A new {@link AuthorizationManager} instance
118-
*/
119-
default AuthorizationManager<T> hasScope(String scope) {
120-
return AuthorityAuthorizationManager.hasScope(scope);
121-
}
122-
123-
/**
124-
* Creates an {@link AuthorizationManager} that requires users to have one of many
125-
* scopes.
126-
* @param scopes the scopes (automatically prepended with SCOPE_) that the user should
127-
* have at least one of to allow access (i.e. write, read, etc.)
128-
* @return A new {@link AuthorizationManager} instance
129-
*/
130-
default AuthorizationManager<T> hasAnyScope(String... scopes) {
131-
return AuthorityAuthorizationManager.hasAnyScope(scopes);
132-
}
133-
134112
/**
135113
* Creates an {@link AuthorizationManager} that allows any authenticated user.
136114
* @return A new {@link AuthorizationManager} instance

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.springframework.security.core.GrantedAuthority;
3030

3131
import static org.assertj.core.api.Assertions.assertThat;
32-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3332
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
3433

3534
/**
@@ -272,24 +271,4 @@ void hasAnyRoleWhenEmptyRolePrefixThenNoException() {
272271
AuthorityAuthorizationManager.hasAnyRole("", new String[] { "USER" });
273272
}
274273

275-
@Test
276-
void hasAnyScopeWhenInvalidScopeThenThrowIllegalArgument() {
277-
String[] scopes = { "read", "write", "SCOPE_invalid" };
278-
assertThatExceptionOfType(IllegalArgumentException.class)
279-
.isThrownBy(() -> AuthorityAuthorizationManager.hasAnyScope(scopes))
280-
.withMessageContaining("SCOPE_invalid should not start with SCOPE_");
281-
}
282-
283-
@Test
284-
void hasScopeWhenValidScope() {
285-
String scope = "read";
286-
assertThat(AuthorityAuthorizationManager.hasScope(scope)).isNotNull();
287-
}
288-
289-
@Test
290-
void hasAnyScopeWhenValidScopes() {
291-
String[] scopes = { "read", "write" };
292-
assertThat(AuthorityAuthorizationManager.hasAnyScope(scopes)).isNotNull();
293-
}
294-
295274
}

core/src/test/java/org/springframework/security/authorization/method/PreAuthorizeReactiveAuthorizationManagerTests.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,42 @@ public void checkDoSomethingStringWhenArgIsNotGrantThenDeniedDecision() throws E
9090
assertThat(decision.isGranted()).isFalse();
9191
}
9292

93+
@Test
94+
public void checkSecuredScope() throws Exception {
95+
MockMethodInvocation methodInvocation = new MockMethodInvocation(
96+
new PreAuthorizeAuthorizationManagerTests.TestClass(),
97+
PreAuthorizeAuthorizationManagerTests.TestClass.class, "securedScope");
98+
PreAuthorizeReactiveAuthorizationManager manager = new PreAuthorizeReactiveAuthorizationManager();
99+
Mono<Authentication> authentication = Mono
100+
.just(new TestingAuthenticationToken("user", "password", "SCOPE_read"));
101+
AuthorizationResult decision = manager.authorize(authentication, methodInvocation).block();
102+
assertThat(decision).isNotNull();
103+
assertThat(decision.isGranted()).isFalse();
104+
105+
authentication = Mono.just(new TestingAuthenticationToken("user", "password", "SCOPE_write"));
106+
decision = manager.authorize(authentication, methodInvocation).block();
107+
assertThat(decision).isNotNull();
108+
assertThat(decision.isGranted()).isTrue();
109+
}
110+
111+
@Test
112+
public void checkSecuredAnyScope() throws Exception {
113+
MockMethodInvocation methodInvocation = new MockMethodInvocation(
114+
new PreAuthorizeAuthorizationManagerTests.TestClass(),
115+
PreAuthorizeAuthorizationManagerTests.TestClass.class, "securedAnyScope");
116+
PreAuthorizeReactiveAuthorizationManager manager = new PreAuthorizeReactiveAuthorizationManager();
117+
Mono<Authentication> authentication = Mono
118+
.just(new TestingAuthenticationToken("user", "password", "SCOPE_read"));
119+
AuthorizationResult decision = manager.authorize(authentication, methodInvocation).block();
120+
assertThat(decision).isNotNull();
121+
assertThat(decision.isGranted()).isTrue();
122+
123+
authentication = Mono.just(new TestingAuthenticationToken("user", "password", "SCOPE_write"));
124+
decision = manager.authorize(authentication, methodInvocation).block();
125+
assertThat(decision).isNotNull();
126+
assertThat(decision.isGranted()).isTrue();
127+
}
128+
93129
@Test
94130
public void checkRequiresAdminWhenClassAnnotationsThenMethodAnnotationsTakePrecedence() throws Exception {
95131
Mono<Authentication> authentication = Mono
@@ -149,6 +185,16 @@ public void inheritedAnnotations() {
149185

150186
}
151187

188+
@PreAuthorize("hasScope('write')")
189+
public void securedScope() {
190+
191+
}
192+
193+
@PreAuthorize("hasAnyScope('write', 'read')")
194+
public void securedAnyScope() {
195+
196+
}
197+
152198
}
153199

154200
@PreAuthorize("hasRole('USER')")

0 commit comments

Comments
 (0)