Class SecurityUtil

java.lang.Object
ch.tocco.nice2.security.api.SecurityUtil

public final class SecurityUtil extends Object
Utility methods dealing with Principal and SecurityManager.
  • Method Details

    • isAuthenticated

      public static boolean isAuthenticated()
      Returns true if the current principal exists and is not anonymous.
      Returns:
      true if non-anonymous principal, false otherwise
    • isAnonymous

      public static boolean isAnonymous()
      Returns true if the current principal exists and is anonymous.
      Returns:
      true if anonymous principal, false otherwise
    • getCurrentUsername

      @Nullable public static @Nullable String getCurrentUsername()
      Returns the username of the currently logged in principal or null if not logged in.
    • getCurrentPrincipal

      @Nullable public static @Nullable Principal getCurrentPrincipal()
      Finds the current principal on the thread. If the thread is not setup correctly (no SecurityContext available) this method returns null.
      Returns:
      the current principal or null
    • requireCurrentPrincipal

      public static Principal requireCurrentPrincipal()
      Always returns a valid principal. Throws an exception, if no principal was found.
      Returns:
      the current principal
    • getSecurityContext

      @Nullable public static @Nullable SecurityContext getSecurityContext()
      Returns the SecurityContext. If it is not available (i.e. the thread is not setup correctly) null is returned.
      Returns:
      the current security context or null
    • getSecurityContextOnValidThread

      @Nullable public static @Nullable SecurityContext getSecurityContextOnValidThread()
      Returns the SecurityContext ensuring a valid thread. It will throw an exception if no ExecutionContext has been set. Otherwise it returns the SecurityContext which may be null.
    • requireSecurityContext

      public static SecurityContext requireSecurityContext()
      Returns the SecurityContext, throwing an exception if it is not available.
    • checkPermission

      public static void checkPermission(Guard g, String... permissions)
      Checks all permissions against the given guard. If one permission fails to authorize, an exception is thrown.
    • checkPermission

      public static void checkPermission(Guard g, Permission... permissions)
      Checks all permissions against the given guard. If one permission fails to authorize, an exception is thrown.
    • hasPermission

      public static boolean hasPermission(Guard g, String... permissions)
      Checks all permissions against the given guard. If one or more permission fails to authorize, false is returned, otherwise true.
    • hasPermission

      public static boolean hasPermission(Guard g, Permission... permissions)
      Checks all permissions against the given guard. If one or more permission fails to authorize, false is returned, otherwise true.