Skip to content

Commit 5d71d2a

Browse files
committed
SEC-1887: Add MethodSecurityOperations interface.
This should cater for implementations which want to use the full filtering capabilities while creating a custom expression root object. Also cleaning whitespace.
1 parent 2434564 commit 5d71d2a

File tree

7 files changed

+83
-83
lines changed

7 files changed

+83
-83
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ protected StandardEvaluationContext createEvaluationContextInternal(Authenticati
7070
protected abstract SecurityExpressionOperations createSecurityExpressionRoot(Authentication authentication, T invocation);
7171

7272
protected RoleHierarchy getRoleHierarchy() {
73-
return roleHierarchy;
74-
}
75-
73+
return roleHierarchy;
74+
}
75+
7676
public void setRoleHierarchy(RoleHierarchy roleHierarchy) {
7777
this.roleHierarchy = roleHierarchy;
7878
}
Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,41 @@
11
package org.springframework.security.access.expression;
22

3+
import org.springframework.security.core.Authentication;
4+
5+
/**
6+
* Standard interface for expression root objects used with expression-based
7+
* security.
8+
*
9+
* @author Andrei Stefan
10+
* @author Luke Taylor
11+
* @since 3.1.1
12+
*/
313
public interface SecurityExpressionOperations {
414

5-
public abstract boolean hasAuthority(String authority);
15+
Authentication getAuthentication();
616

7-
public abstract boolean hasAnyAuthority(String... authorities);
17+
boolean hasAuthority(String authority);
818

9-
public abstract boolean hasRole(String role);
19+
boolean hasAnyAuthority(String... authorities);
1020

11-
public abstract boolean hasAnyRole(String... roles);
21+
boolean hasRole(String role);
1222

13-
public abstract boolean permitAll();
23+
boolean hasAnyRole(String... roles);
1424

15-
public abstract boolean denyAll();
25+
boolean permitAll();
1626

17-
public abstract boolean isAnonymous();
27+
boolean denyAll();
1828

19-
public abstract boolean isAuthenticated();
29+
boolean isAnonymous();
2030

21-
public abstract boolean isRememberMe();
31+
boolean isAuthenticated();
2232

23-
public abstract boolean isFullyAuthenticated();
33+
boolean isRememberMe();
2434

25-
public abstract boolean hasPermission(Object target, Object permission);
35+
boolean isFullyAuthenticated();
2636

27-
public abstract boolean hasPermission(Object targetId, String targetType,
28-
Object permission);
37+
boolean hasPermission(Object target, Object permission);
2938

30-
}
39+
boolean hasPermission(Object targetId, String targetType, Object permission);
40+
41+
}

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

Lines changed: 13 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -45,35 +45,19 @@ public SecurityExpressionRoot(Authentication a) {
4545
this.authentication = a;
4646
}
4747

48-
/* (non-Javadoc)
49-
* @see org.springframework.security.access.expression.SecurityExpressionOperations#hasAuthority(java.lang.String)
50-
*/
51-
@Override
52-
public final boolean hasAuthority(String authority) {
48+
public final boolean hasAuthority(String authority) {
5349
return hasRole(authority);
5450
}
5551

56-
/* (non-Javadoc)
57-
* @see org.springframework.security.access.expression.SecurityExpressionOperations#hasAnyAuthority(java.lang.String)
58-
*/
59-
@Override
60-
public final boolean hasAnyAuthority(String... authorities) {
52+
public final boolean hasAnyAuthority(String... authorities) {
6153
return hasAnyRole(authorities);
6254
}
6355

64-
/* (non-Javadoc)
65-
* @see org.springframework.security.access.expression.SecurityExpressionOperations#hasRole(java.lang.String)
66-
*/
67-
@Override
68-
public final boolean hasRole(String role) {
56+
public final boolean hasRole(String role) {
6957
return getAuthoritySet().contains(role);
7058
}
7159

72-
/* (non-Javadoc)
73-
* @see org.springframework.security.access.expression.SecurityExpressionOperations#hasAnyRole(java.lang.String)
74-
*/
75-
@Override
76-
public final boolean hasAnyRole(String... roles) {
60+
public final boolean hasAnyRole(String... roles) {
7761
Set<String> roleSet = getAuthoritySet();
7862

7963
for (String role : roles) {
@@ -89,51 +73,27 @@ public final Authentication getAuthentication() {
8973
return authentication;
9074
}
9175

92-
/* (non-Javadoc)
93-
* @see org.springframework.security.access.expression.SecurityExpressionOperations#permitAll()
94-
*/
95-
@Override
96-
public final boolean permitAll() {
76+
public final boolean permitAll() {
9777
return true;
9878
}
9979

100-
/* (non-Javadoc)
101-
* @see org.springframework.security.access.expression.SecurityExpressionOperations#denyAll()
102-
*/
103-
@Override
104-
public final boolean denyAll() {
80+
public final boolean denyAll() {
10581
return false;
10682
}
10783

108-
/* (non-Javadoc)
109-
* @see org.springframework.security.access.expression.SecurityExpressionOperations#isAnonymous()
110-
*/
111-
@Override
112-
public final boolean isAnonymous() {
84+
public final boolean isAnonymous() {
11385
return trustResolver.isAnonymous(authentication);
11486
}
11587

116-
/* (non-Javadoc)
117-
* @see org.springframework.security.access.expression.SecurityExpressionOperations#isAuthenticated()
118-
*/
119-
@Override
120-
public final boolean isAuthenticated() {
88+
public final boolean isAuthenticated() {
12189
return !isAnonymous();
12290
}
12391

124-
/* (non-Javadoc)
125-
* @see org.springframework.security.access.expression.SecurityExpressionOperations#isRememberMe()
126-
*/
127-
@Override
128-
public final boolean isRememberMe() {
92+
public final boolean isRememberMe() {
12993
return trustResolver.isRememberMe(authentication);
13094
}
13195

132-
/* (non-Javadoc)
133-
* @see org.springframework.security.access.expression.SecurityExpressionOperations#isFullyAuthenticated()
134-
*/
135-
@Override
136-
public final boolean isFullyAuthenticated() {
96+
public final boolean isFullyAuthenticated() {
13797
return !trustResolver.isAnonymous(authentication) && !trustResolver.isRememberMe(authentication);
13898
}
13999

@@ -164,19 +124,12 @@ private Set<String> getAuthoritySet() {
164124
return roles;
165125
}
166126

167-
/* (non-Javadoc)
168-
* @see org.springframework.security.access.expression.SecurityExpressionOperations#hasPermission(java.lang.Object, java.lang.Object)
169-
*/
170-
@Override
171-
public boolean hasPermission(Object target, Object permission) {
127+
128+
public boolean hasPermission(Object target, Object permission) {
172129
return permissionEvaluator.hasPermission(authentication, target, permission);
173130
}
174131

175-
/* (non-Javadoc)
176-
* @see org.springframework.security.access.expression.SecurityExpressionOperations#hasPermission(java.lang.Object, java.lang.String, java.lang.Object)
177-
*/
178-
@Override
179-
public boolean hasPermission(Object targetId, String targetType, Object permission) {
132+
public boolean hasPermission(Object targetId, String targetType, Object permission) {
180133
return permissionEvaluator.hasPermission(authentication, (Serializable)targetId, targetType, permission);
181134
}
182135

core/src/main/java/org/springframework/security/access/expression/method/DefaultMethodSecurityExpressionHandler.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ public StandardEvaluationContext createEvaluationContextInternal(Authentication
4848
return new MethodSecurityEvaluationContext(auth, mi, parameterNameDiscoverer);
4949
}
5050

51-
@Override
52-
protected SecurityExpressionOperations createSecurityExpressionRoot(Authentication authentication, MethodInvocation invocation) {
51+
/**
52+
* Creates the root object for expression evaluation.
53+
*/
54+
protected MethodSecurityExpressionOperations createSecurityExpressionRoot(Authentication authentication, MethodInvocation invocation) {
5355
MethodSecurityExpressionRoot root = new MethodSecurityExpressionRoot(authentication);
5456
root.setThis(invocation.getThis());
5557
root.setPermissionEvaluator(getPermissionEvaluator());
@@ -68,7 +70,7 @@ protected SecurityExpressionOperations createSecurityExpressionRoot(Authenticati
6870
*/
6971
@SuppressWarnings("unchecked")
7072
public Object filter(Object filterTarget, Expression filterExpression, EvaluationContext ctx) {
71-
MethodSecurityExpressionRoot rootObject = (MethodSecurityExpressionRoot) ctx.getRootObject().getValue();
73+
MethodSecurityExpressionOperations rootObject = (MethodSecurityExpressionOperations) ctx.getRootObject().getValue();
7274
final boolean debug = logger.isDebugEnabled();
7375
List retainList;
7476

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2006-2011 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package org.springframework.security.access.expression.method;
14+
15+
import org.springframework.security.access.expression.SecurityExpressionOperations;
16+
17+
/**
18+
* Interface which must be implemented if you want to use filtering in method security
19+
* expressions.
20+
*
21+
* @author Luke Taylor
22+
* @since 3.1.1
23+
*/
24+
public interface MethodSecurityExpressionOperations extends SecurityExpressionOperations {
25+
void setFilterObject(Object filterObject);
26+
27+
Object getFilterObject();
28+
29+
void setReturnObject(Object returnObject);
30+
31+
Object getReturnObject();
32+
33+
Object getThis();
34+
}

core/src/main/java/org/springframework/security/access/expression/method/MethodSecurityExpressionRoot.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @author Luke Taylor
1111
* @since 3.0
1212
*/
13-
class MethodSecurityExpressionRoot extends SecurityExpressionRoot {
13+
class MethodSecurityExpressionRoot extends SecurityExpressionRoot implements MethodSecurityExpressionOperations {
1414
private Object filterObject;
1515
private Object returnObject;
1616
private Object target;

web/src/main/java/org/springframework/security/web/access/expression/DefaultWebSecurityExpressionHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
* @since 3.0
1414
*/
1515
public class DefaultWebSecurityExpressionHandler extends AbstractSecurityExpressionHandler<FilterInvocation> {
16-
17-
private final AuthenticationTrustResolver trustResolver = new AuthenticationTrustResolverImpl();
16+
17+
private final AuthenticationTrustResolver trustResolver = new AuthenticationTrustResolverImpl();
1818

1919
@Override
2020
protected SecurityExpressionOperations createSecurityExpressionRoot(Authentication authentication, FilterInvocation fi) {

0 commit comments

Comments
 (0)