From 709b24257f36d16321c69c3d8ff21c31fcec8b21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ambrus=20Adri=C3=A1n-Zolt=C3=A1n?= Date: Sun, 23 Oct 2016 21:41:28 +0300 Subject: [PATCH] Further simplified the vote method. --- .../org/baeldung/security/MinuteBasedVoter.java | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/spring-security-custom-voter/src/main/java/org/baeldung/security/MinuteBasedVoter.java b/spring-security-custom-voter/src/main/java/org/baeldung/security/MinuteBasedVoter.java index 8d22c52b0d..e0f42aa16e 100644 --- a/spring-security-custom-voter/src/main/java/org/baeldung/security/MinuteBasedVoter.java +++ b/spring-security-custom-voter/src/main/java/org/baeldung/security/MinuteBasedVoter.java @@ -23,16 +23,13 @@ public class MinuteBasedVoter implements AccessDecisionVoter { @Override public int vote(Authentication authentication, Object object, Collection collection) { - List roles = authentication + return authentication .getAuthorities() - .stream().map(GrantedAuthority::getAuthority) - .collect(Collectors.toList()); - - for (String role: roles) { - if ("ROLE_USER".equals(role) && LocalDateTime.now().getMinute() % 2 != 0) { - return ACCESS_DENIED; - } - } - return ACCESS_ABSTAIN; + .stream() + .map(GrantedAuthority::getAuthority) + .filter(r -> "ROLE_USER".equals(r) && LocalDateTime.now().getMinute() % 2 != 0) + .findAny() + .map(s -> ACCESS_DENIED) + .orElseGet(() -> ACCESS_ABSTAIN); } }