Skip to content

Commit ba115bb

Browse files
authored
feat: allow defining access control annotations on view superclasses (#11097)
This allows leaving child views unannotated with any of the access annotations and the access rules to be defined by the parent view classes. Fixes: #10705
1 parent 1a057da commit ba115bb

3 files changed

Lines changed: 314 additions & 10 deletions

File tree

‎flow-server/src/main/java/com/vaadin/flow/server/auth/AccessAnnotationChecker.java‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.lang.reflect.Method;
2121
import java.lang.reflect.Modifier;
2222
import java.security.Principal;
23+
import java.util.Objects;
2324
import java.util.function.Function;
2425

2526
import javax.annotation.security.DenyAll;
@@ -199,12 +200,37 @@ public AnnotatedElement getSecurityTarget(Method method) {
199200
*
200201
* @param cls
201202
* the class to check
202-
* @return the entity that is responsible for security settings for the
203-
* method passed
204-
* @throws IllegalArgumentException
205-
* if the method is not public
203+
* @return the first annotated class in {@code cls}'s hierarchy that
204+
* annotated with one of the access annotations, starting from the
205+
* input {@code cls} class itself, going up in the hierarchy.
206+
* <em>Note:</em> interfaces in the {@code cls}'s hierarchy are
207+
* ignored.
208+
* <p>
209+
* If no class in the hierarchy was annotated with any of the access
210+
* annotations, the {@code cls} input parameter itself would be
211+
* returned.
212+
* <p>
213+
* Access annotations that being checked are:
214+
* <ul>
215+
* <li>{@code @AnonymousAllowed}
216+
* <li>{@code @PermitAll}
217+
* <li>{@code @RolesAllowed}
218+
* <li>{@code @DenyAll}
219+
* </ul>
220+
*
221+
* @throws NullPointerException
222+
* if the input {@code cls} is null
206223
*/
207224
public AnnotatedElement getSecurityTarget(Class<?> cls) {
225+
Objects.requireNonNull(cls, "The input Class must not be null.");
226+
227+
Class<?> clazz = cls;
228+
while (clazz != null && clazz != Object.class) {
229+
if (hasSecurityAnnotation(clazz)) {
230+
return clazz;
231+
}
232+
clazz = clazz.getSuperclass();
233+
}
208234
return cls;
209235
}
210236

@@ -240,7 +266,7 @@ private boolean roleAllowed(RolesAllowed rolesAllowed,
240266
return false;
241267
}
242268

243-
private boolean hasSecurityAnnotation(Method method) {
269+
private boolean hasSecurityAnnotation(AnnotatedElement method) {
244270
return method.isAnnotationPresent(AnonymousAllowed.class)
245271
|| method.isAnnotationPresent(PermitAll.class)
246272
|| method.isAnnotationPresent(DenyAll.class)

‎flow-server/src/test/java/com/vaadin/flow/server/auth/AccessControlTestClasses.java‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,4 +270,82 @@ public static class RolesAllowedUserView extends Component {
270270
@Route("admin")
271271
public static class RolesAllowedAdminView extends Component {
272272
}
273+
274+
@AnonymousAllowed
275+
public static class AnonymousAllowedParentView extends Component {
276+
}
277+
278+
@Route("no-annotation-anonymous-by-parent")
279+
public static class NoAnnotationAnonymousAllowedByParentView
280+
extends AnonymousAllowedParentView {
281+
}
282+
283+
@Route("no-annotation-anonymous-by-grandparent")
284+
public static class NoAnnotationAnonymousAllowedByGrandParentView
285+
extends NoAnnotationAnonymousAllowedByParentView {
286+
}
287+
288+
@PermitAll
289+
public static class PermitAllGrandParentView {
290+
}
291+
292+
public static class PermitAllParentView extends PermitAllGrandParentView {
293+
}
294+
295+
@Route("no-annotation-permitall-by-grandparent")
296+
public static class NoAnnotationPermitAllByGrandParentView
297+
extends PermitAllParentView {
298+
}
299+
300+
@RolesAllowed("user")
301+
public static class RolesAllowedUserGrandParentView {
302+
}
303+
304+
public static class RolesAllowedUserParentView
305+
extends RolesAllowedUserGrandParentView {
306+
}
307+
308+
@Route("no-annotation-roles-allowed-user-by-grandparent")
309+
public static class NoAnnotationRolesAllowedUserByGrandParentView
310+
extends RolesAllowedUserParentView {
311+
}
312+
313+
@RolesAllowed("admin")
314+
public static class RolesAllowedAdminGrandParentView {
315+
}
316+
317+
public static class RolesAllowedAdminParentView
318+
extends RolesAllowedAdminGrandParentView {
319+
}
320+
321+
@Route("no-annotation-roles-allowed-admin-by-grandparent")
322+
public static class NoAnnotationRolesAllowedAdminByGrandParentView
323+
extends RolesAllowedAdminParentView {
324+
}
325+
326+
@DenyAll
327+
public static class DenyAllGrandParentView {
328+
}
329+
330+
public static class DenyAllParentView extends DenyAllGrandParentView {
331+
}
332+
333+
@Route("no-annotation-denyall-by-grandparent")
334+
public static class NoAnnotationDenyAllByGrandParentView
335+
extends DenyAllParentView {
336+
}
337+
338+
@AnonymousAllowed
339+
public interface CustomComponent {
340+
}
341+
342+
@Route("no-annotation-denyall-as-interfaces-ignored")
343+
public static class NoAnnotationDenyAllAsInterfacesIgnoredView
344+
implements CustomComponent {
345+
}
346+
347+
@Route("no-annotation-permitall-from-parent-and-interfaces-ignored")
348+
public static class NoAnnotationPermitAllByGrandParentAsInterfacesIgnoredView
349+
extends PermitAllParentView implements CustomComponent {
350+
}
273351
}

0 commit comments

Comments
 (0)