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 ; }
Returns whether the current user has at least one of the given roles