Skip to content

Commit 1e53355

Browse files
author
Ambrus Adrián-Zoltán
committed
Changed collect, forEach call to filter, findAny, orElseGet
1 parent a22daf9 commit 1e53355

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

‎spring-security-custom-voter/src/main/java/org/baeldung/security/MinuteBasedVoter.java‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,17 @@ public boolean supports(Class clazz) {
2323

2424
@Override
2525
public int vote(Authentication authentication, Object object, Collection collection) {
26-
List<String> roles = authentication
26+
String role = authentication
2727
.getAuthorities()
2828
.stream().map(GrantedAuthority::getAuthority)
29-
.collect(Collectors.toList());
29+
.filter("ROLE_USER"::equals)
30+
.findAny()
31+
.orElseGet(() -> "ROLE_ADMIN");
3032

31-
for (String role: roles) {
32-
if ("ROLE_USER".equals(role) && LocalDateTime.now().getMinute() % 2 != 0) {
33+
if ("ROLE_USER".equals(role) && LocalDateTime.now().getMinute() % 2 != 0) {
3334
return ACCESS_DENIED;
34-
}
3535
}
36+
3637
return ACCESS_ABSTAIN;
3738
}
3839
}

0 commit comments

Comments
 (0)