/** * Check if current user has specified role. * * @param privilege the role to check if user has. * @return true if user has specified role, otherwise false. */ public static boolean hasPrivilege(final String privilege) { final UserDetails userDetails = SpringSecurityUtil.getCurrentUserDetails(); if (userDetails != null) { for (final GrantedAuthority each : userDetails.getAuthorities()) { if (each.getAuthority().equals(privilege)) { return true; } } } return false; }
/** * Check if current user has any role of specified. * * @param privileges the array of roles. * @return true if has any role, otherwise false. */ public static boolean hasAnyPrivilege(final String... privileges) { final UserDetails userDetails = SpringSecurityUtil.getCurrentUserDetails(); if (userDetails != null) { final Set<String> rolesSet = ImmutableSet.copyOf(privileges); for (final GrantedAuthority each : userDetails.getAuthorities()) { if (rolesSet.contains(each.getAuthority())) { return true; } } } return false; }
/** * Check if current user is anonymous guest. * * @return true if user is anonymous guest. */ public static boolean isAnonymous() { return SpringSecurityUtil.getCurrentUserDetails() == null; }
/** * Check if current user is authenticated. * * @return true if user is authenticated. */ public static boolean isAuthenticated() { return SpringSecurityUtil.getCurrentUserDetails() != null; }