Class SecurityUtil
java.lang.Object
ch.tocco.nice2.security.api.SecurityUtil
Utility methods dealing with
Principal and SecurityManager.-
Method Summary
Modifier and TypeMethodDescriptionstatic voidcheckPermission(Guard g, Permission... permissions) Checks all permissions against the given guard.static voidcheckPermission(Guard g, String... permissions) Checks all permissions against the given guard.static @Nullable PrincipalFinds the current principal on the thread.static @Nullable StringReturns the username of the currently logged in principal ornullif not logged in.static @Nullable SecurityContextReturns theSecurityContext.static @Nullable SecurityContextReturns theSecurityContextensuring a valid thread.static booleanhasPermission(Guard g, Permission... permissions) Checks all permissions against the given guard.static booleanhasPermission(Guard g, String... permissions) Checks all permissions against the given guard.static booleanReturnstrueif the current principal exists and is anonymous.static booleanReturnstrueif the current principal exists and is not anonymous.static PrincipalAlways returns a valid principal.static SecurityContextReturns theSecurityContext, throwing an exception if it is not available.
-
Method Details
-
isAuthenticated
public static boolean isAuthenticated()Returnstrueif the current principal exists and is not anonymous.- Returns:
trueif non-anonymous principal,falseotherwise
-
isAnonymous
public static boolean isAnonymous()Returnstrueif the current principal exists and is anonymous.- Returns:
trueif anonymous principal,falseotherwise
-
getCurrentUsername
Returns the username of the currently logged in principal ornullif not logged in. -
getCurrentPrincipal
Finds the current principal on the thread. If the thread is not setup correctly (noSecurityContextavailable) this method returnsnull.- Returns:
- the current principal or
null
-
requireCurrentPrincipal
Always returns a valid principal. Throws an exception, if no principal was found.- Returns:
- the current principal
-
getSecurityContext
Returns theSecurityContext. If it is not available (i.e. the thread is not setup correctly)nullis returned.- Returns:
- the current security context or
null
-
getSecurityContextOnValidThread
Returns theSecurityContextensuring a valid thread. It will throw an exception if noExecutionContexthas been set. Otherwise it returns theSecurityContextwhich may benull. -
requireSecurityContext
Returns theSecurityContext, throwing an exception if it is not available. -
checkPermission
-
checkPermission
Checks all permissions against the given guard. If one permission fails to authorize, an exception is thrown. -
hasPermission
-
hasPermission
Checks all permissions against the given guard. If one or more permission fails to authorize,falseis returned, otherwisetrue.
-