@Test void isUserInRole() { assertTrue(SecurityUtils.currentUserHasRole("authority1")); assertTrue(SecurityUtils.currentUserHasRole("authority2")); assertTrue(SecurityUtils.currentUserHasRole("authority1", "authority2")); assertTrue(SecurityUtils.currentUserHasRole("authority2", "authority1")); assertTrue(SecurityUtils.currentUserHasRole("authority1", "authority3")); }
public static boolean currentUserHasRole(String... roles) { if (roles == null || roles.length == 0) return false; Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication != null) { Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities(); if (authorities == null) throw new IllegalStateException("No user currently logged in"); for (String role : roles) { for (GrantedAuthority grantedAuthority : authorities) { if (role.equals(grantedAuthority.getAuthority())) return true; } } } return false; }
SecurityUtils { public static boolean currentUserHasRole(String... roles) { if (roles == null || roles.length == 0) return false; Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication != null) { Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities(); if (authorities == null) throw new IllegalStateException("No user currently logged in"); for (String role : roles) { for (GrantedAuthority grantedAuthority : authorities) { if (role.equals(grantedAuthority.getAuthority())) return true; } } } return false; } }
SecurityUtils { public static boolean currentUserHasRole(String... roles) { if (roles == null || roles.length == 0) return false; Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication != null) { Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities(); if (authorities == null) throw new IllegalStateException("No user currently logged in"); for (String role : roles) { for (GrantedAuthority grantedAuthority : authorities) { if (role.equals(grantedAuthority.getAuthority())) return true; } } } return false; } private  SecurityUtils(); }
SecurityUtils { public static boolean currentUserHasRole(String... roles) { if (roles == null || roles.length == 0) return false; Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication != null) { Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities(); if (authorities == null) throw new IllegalStateException("No user currently logged in"); for (String role : roles) { for (GrantedAuthority grantedAuthority : authorities) { if (role.equals(grantedAuthority.getAuthority())) return true; } } } return false; } private  SecurityUtils(); static @Nullable @CheckForNull String getCurrentUsername(); static boolean currentUserHasRole(String... roles); static boolean currentUserIsSuOrSystem(); static boolean currentUserIsSu(); static boolean currentUserIsSystem(); static boolean currentUserIsAuthenticated(); static boolean currentUserIsAnonymous(); }
SecurityUtils { public static boolean currentUserHasRole(String... roles) { if (roles == null || roles.length == 0) return false; Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication != null) { Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities(); if (authorities == null) throw new IllegalStateException("No user currently logged in"); for (String role : roles) { for (GrantedAuthority grantedAuthority : authorities) { if (role.equals(grantedAuthority.getAuthority())) return true; } } } return false; } private  SecurityUtils(); static @Nullable @CheckForNull String getCurrentUsername(); static boolean currentUserHasRole(String... roles); static boolean currentUserIsSuOrSystem(); static boolean currentUserIsSu(); static boolean currentUserIsSystem(); static boolean currentUserIsAuthenticated(); static boolean currentUserIsAnonymous(); static final String ANONYMOUS_USERNAME; static final String AUTHORITY_SU; static final String AUTHORITY_ANONYMOUS; static final String AUTHORITY_USER; static final String ROLE_SYSTEM; static final String ROLE_ACL_TAKE_OWNERSHIP; static final String ROLE_ACL_MODIFY_AUDITING; static final String ROLE_ACL_GENERAL_CHANGES; }