public class Security extends Object
getInstance()
and call the desired method, or inject as a dependency.
IMPORTANT: New methods added to this class should be non-static.
Modifier and Type | Method and Description |
---|---|
Date |
getExpires(Subject subject)
Get the expires time from the
Subject 's token. |
Subject |
getGuestSubject(String ipAddress)
Gets the guest
Subject associated with the specified IP. |
static Security |
getInstance() |
SecurityManager |
getSecurityManager()
Gets a reference to the
SecurityManager . |
Subject |
getSubject(String username,
String password)
Gets the
Subject given a user name and password. |
Subject |
getSystemSubject()
Gets the
Subject associated with this system. |
boolean |
javaSubjectHasAdminRole()
Determines if the current Java
Subject has the admin role. |
<T> T |
runAsAdmin(PrivilegedAction<T> action) |
<T> T |
runAsAdminWithException(PrivilegedExceptionAction<T> action) |
<T> T |
runWithSubjectOrElevate(Callable<T> codeToRun)
|
boolean |
tokenAboutToExpire(Subject subject)
Determines whether a
Subject 's token is about to expire or not. |
public static Security getInstance()
null
.public Subject getSubject(String username, String password)
Subject
given a user name and password.username
- usernamepassword
- passwordSubject
associated with the user name and password providedpublic boolean javaSubjectHasAdminRole()
Subject
has the admin role.true
if the Java Subject
exists and has the admin role, false
otherwisepublic <T> T runWithSubjectOrElevate(@NotNull Callable<T> codeToRun) throws SecurityServiceException, InvocationTargetException
Callable
in the current thread as the current security framework's Subject
. If the security framework's Subject
is not currently set and the Java Subject
contains the admin role, elevates and runs the Callable
as the system Subject
.T
- type of the returned valuecodeToRun
- code to runCallable
SecurityServiceException
- if the current subject didn' have enough permissions to run
the codeInvocationTargetException
- wraps any exception thrown by Callable.call()
. Callable
exception can be retrieved using the InvocationTargetException.getCause()
.@Nullable public Subject getSystemSubject()
Subject
associated with this system. Uses a cached subject since the subject
will not change between calls.public Subject getGuestSubject(String ipAddress)
Subject
associated with the specified IP. Uses a cached subject when
possible since the subject will not change between calls.Subject
public boolean tokenAboutToExpire(Subject subject)
Subject
's token is about to expire or not.subject
- subject whose token needs to be checkedtrue
only if the Subject
's token will expire soonpublic Date getExpires(Subject subject)
Subject
's token.subject
- subject whose token needs to be checkedDate
or null if subject doesn't have an expire time.@Nullable public SecurityManager getSecurityManager()
SecurityManager
.SecurityManager
or null
if unable to get the SecurityManager
public <T> T runAsAdmin(PrivilegedAction<T> action)
public <T> T runAsAdminWithException(PrivilegedExceptionAction<T> action) throws PrivilegedActionException
PrivilegedActionException
This work is licensed under a Creative Commons Attribution 4.0 International License.