Skip to content

Commit 636ac64

Browse files
Add reasons to AuthorizationDecisions
Closes gh-9287
1 parent f9170a1 commit 636ac64

8 files changed

+108
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.authorization;
18+
19+
import java.util.Collection;
20+
21+
/**
22+
* Represents an {@link AuthorizationDecision} based on a collection of authorities
23+
*
24+
* @author Marcus Da Coregio
25+
* @since 5.6
26+
*/
27+
class AuthorityAuthorizationDecision extends AuthorizationDecision {
28+
29+
private final Collection<String> authorities;
30+
31+
AuthorityAuthorizationDecision(boolean granted, Collection<String> authorities) {
32+
super(granted);
33+
this.authorities = authorities;
34+
}
35+
36+
Collection<String> getAuthorities() {
37+
return this.authorities;
38+
}
39+
40+
@Override
41+
public String toString() {
42+
return getClass().getSimpleName() + " [" + "granted=" + isGranted() + ", authorities=" + this.authorities + ']';
43+
}
44+
45+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ private static String[] toNamedRolesArray(String rolePrefix, String[] roles) {
124124
@Override
125125
public AuthorizationDecision check(Supplier<Authentication> authentication, T object) {
126126
boolean granted = isGranted(authentication.get());
127-
return new AuthorizationDecision(granted);
127+
return new AuthorityAuthorizationDecision(granted, this.authorities);
128128
}
129129

130130
private boolean isGranted(Authentication authentication) {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public Mono<AuthorizationDecision> check(Mono<Authentication> authentication, T
4848
.flatMapIterable(Authentication::getAuthorities)
4949
.map(GrantedAuthority::getAuthority)
5050
.any(this.authorities::contains)
51-
.map(AuthorizationDecision::new)
52-
.defaultIfEmpty(new AuthorizationDecision(false));
51+
.map((granted) -> ((AuthorizationDecision) new AuthorityAuthorizationDecision(granted, this.authorities)))
52+
.defaultIfEmpty(new AuthorityAuthorizationDecision(false, this.authorities));
5353
// @formatter:on
5454
}
5555

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 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.
@@ -32,4 +32,9 @@ public boolean isGranted() {
3232
return this.granted;
3333
}
3434

35+
@Override
36+
public String toString() {
37+
return getClass().getSimpleName() + " [granted=" + this.granted + "]";
38+
}
39+
3540
}

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

+6
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,10 @@ Expression getExpression() {
4949
return this.expression;
5050
}
5151

52+
@Override
53+
public String toString() {
54+
return getClass().getSimpleName() + " [Expression="
55+
+ ((this.expression != null) ? this.expression.getExpressionString() : null) + "]";
56+
}
57+
5258
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.authorization.method;
18+
19+
import org.springframework.security.authorization.AuthorizationDecision;
20+
21+
/**
22+
* Represents an {@link AuthorizationDecision} based on a {@link ExpressionAttribute}
23+
*
24+
* @author Marcus Da Coregio
25+
* @since 5.6
26+
*/
27+
class ExpressionAttributeAuthorizationDecision extends AuthorizationDecision {
28+
29+
private final ExpressionAttribute expressionAttribute;
30+
31+
ExpressionAttributeAuthorizationDecision(boolean granted, ExpressionAttribute expressionAttribute) {
32+
super(granted);
33+
this.expressionAttribute = expressionAttribute;
34+
}
35+
36+
ExpressionAttribute getExpressionAttribute() {
37+
return this.expressionAttribute;
38+
}
39+
40+
@Override
41+
public String toString() {
42+
return getClass().getSimpleName() + " [" + "granted=" + isGranted() + ", expressionAttribute="
43+
+ this.expressionAttribute + ']';
44+
}
45+
46+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public AuthorizationDecision check(Supplier<Authentication> authentication, Meth
7676
mi.getMethodInvocation());
7777
this.expressionHandler.setReturnObject(mi.getResult(), ctx);
7878
boolean granted = ExpressionUtils.evaluateAsBoolean(attribute.getExpression(), ctx);
79-
return new AuthorizationDecision(granted);
79+
return new ExpressionAttributeAuthorizationDecision(granted, attribute);
8080
}
8181

8282
private final class PostAuthorizeExpressionAttributeRegistry

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public AuthorizationDecision check(Supplier<Authentication> authentication, Meth
7474
}
7575
EvaluationContext ctx = this.expressionHandler.createEvaluationContext(authentication.get(), mi);
7676
boolean granted = ExpressionUtils.evaluateAsBoolean(attribute.getExpression(), ctx);
77-
return new AuthorizationDecision(granted);
77+
return new ExpressionAttributeAuthorizationDecision(granted, attribute);
7878
}
7979

8080
private final class PreAuthorizeExpressionAttributeRegistry

0 commit comments

Comments
 (0)