Class SecurityUtils
java.lang.Object
net.solarnetwork.central.security.SecurityUtils
Security helper methods.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidauthenticate(org.springframework.security.authentication.AuthenticationManager authenticationManager, Object username, Object password) Authenticate a user.static Long[]authorizedNodeIds(org.springframework.security.core.Authentication auth, SolarNodeOwnershipDao nodeOwnershipDao) Get all node IDs the given authentication is authorized to access.static Long[]authorizedNodeIdsForCurrentActor(SolarNodeOwnershipDao nodeOwnershipDao) Get all node IDs the current actor is authorized to access.static SecurityNodebecomeNode(Long nodeId) Become a node with aRUN_AS_ROLE_NODEauthority.static SecurityTokenbecomeToken(String tokenId, SecurityTokenType type, Long userId, SecurityPolicy policy) Become an authenticated token with aRUN_AS_ROLE_USERauthority.static SecurityUserbecomeUser(String username, String name, Long userId) Become a user with aRUN_AS_ROLE_USERauthority.static StringGet the currentSecurityToken.getToken(), if available.static SecurityPolicyGet aSecurityPolicyfor the active user, if available.static SecurityActorgetActor(org.springframework.security.core.Authentication auth) Get the actor for a given authentication.static LonggetActorUserId(org.springframework.security.core.Authentication auth) Get the ID of the user associated with a given authentication.static SecurityActorGet the currentSecurityActor.static LongGet the currentSecurityActor'suserId.static org.springframework.security.core.AuthenticationGet the current active authentication.static SecurityNodeGet the currentSecurityNode.static SecurityTokenGet the currentSecurityToken.static SecurityUserGet the currentSecurityUser.static SecurityNodegetNode(org.springframework.security.core.Authentication auth) Get aSecurityNodefor a given authentication.static SecurityTokengetToken(org.springframework.security.core.Authentication auth) Get aSecurityTokenfor a given authentication.static SecurityUsergetUser(org.springframework.security.core.Authentication auth) Get aSecurityUserfor a given authentication.static voidClear the current authentication.static voidrequireAllRoles(Set<String> roles) Require any one of a set of roles for the current actor.static voidrequireAnyRole(Set<String> roles) Require any one of a set of roles for the current actor.
-
Constructor Details
-
SecurityUtils
public SecurityUtils()
-
-
Method Details
-
authenticate
public static void authenticate(org.springframework.security.authentication.AuthenticationManager authenticationManager, Object username, Object password) Authenticate a user.- Parameters:
authenticationManager- theAuthenticationManagerusername- the username to authenticatepassword- the password to authenticate
-
removeAuthentication
public static void removeAuthentication()Clear the current authentication.- Since:
- 2.2
-
becomeToken
public static SecurityToken becomeToken(String tokenId, SecurityTokenType type, Long userId, SecurityPolicy policy) Become an authenticated token with aRUN_AS_ROLE_USERauthority.- Parameters:
tokenId- the token ID to usetype- the token typeuserId- the user IDpolicy- the security policy to use- Since:
- 2.0
-
becomeUser
Become a user with aRUN_AS_ROLE_USERauthority.- Parameters:
username- the username (email) to usename- the nameuserId- the user ID- Since:
- 2.0
-
becomeNode
Become a node with aRUN_AS_ROLE_NODEauthority.- Parameters:
nodeId- the node ID to become- Since:
- 1.4
-
requireAnyRole
Require any one of a set of roles for the current actor. The actor's roles are converted to upper case before testing for inclusion in therolesargument.- Parameters:
roles- the roles, one of which is required- Since:
- 1.2
-
requireAllRoles
Require any one of a set of roles for the current actor. The actor's roles are converted to upper case before testing for inclusion in therolesargument.- Parameters:
roles- the required roles- Since:
- 1.2
-
getCurrentAuthentication
public static org.springframework.security.core.Authentication getCurrentAuthentication()Get the current active authentication.- Returns:
- the active Authentication, or null if none available
-
getCurrentActor
Get the currentSecurityActor.- Returns:
- the current actor, never null
- Throws:
SecurityException- if the actor is not available
-
getActor
Get the actor for a given authentication.- Parameters:
auth- the authentication- Returns:
- the actor, never null
- Throws:
SecurityException- if the actor is not available- Since:
- 2.1
-
getCurrentActorUserId
Get the currentSecurityActor'suserId.- Returns:
- The user ID of the current
SecurityActor(never null). - Throws:
SecurityException- If the user ID is not available.- Since:
- 1.3
-
getActorUserId
public static Long getActorUserId(org.springframework.security.core.Authentication auth) throws SecurityException Get the ID of the user associated with a given authentication.- Parameters:
auth- the authentication- Returns:
- the ID of the user associated with the actor, never null
- Throws:
SecurityException- if the user ID is not available- Since:
- 2.1
-
getCurrentToken
Get the currentSecurityToken.- Returns:
- the current actor, never null
- Throws:
SecurityException- if the actor is not available
-
getToken
public static SecurityToken getToken(org.springframework.security.core.Authentication auth) throws SecurityException Get aSecurityTokenfor a given authentication.- Parameters:
auth- the authentication- Returns:
- the token actor, never null
- Throws:
SecurityException- if the actor is not available or not a token- Since:
- 2.1
-
currentTokenId
Get the currentSecurityToken.getToken(), if available.- Returns:
- the token, or null if a token is not available
- Since:
- 2.2
-
getCurrentUser
Get the currentSecurityUser.- Returns:
- the current user, never null
- Throws:
SecurityException- if the user is not available
-
getUser
public static SecurityUser getUser(org.springframework.security.core.Authentication auth) throws SecurityException Get aSecurityUserfor a given authentication.- Parameters:
auth- the authentication- Returns:
- the user actor, never null
- Throws:
SecurityException- if the actor is not available or is not a user- Since:
- 2.1
-
getCurrentNode
Get the currentSecurityNode.- Returns:
- the current node, never null
- Throws:
SecurityException- if the node is not available
-
getNode
public static SecurityNode getNode(org.springframework.security.core.Authentication auth) throws SecurityException Get aSecurityNodefor a given authentication.- Parameters:
auth- the authentication- Returns:
- the node actor, never null
- Throws:
SecurityException- if the actor is not available or is not a node- Since:
- 2.1
-
authorizedNodeIdsForCurrentActor
Get all node IDs the current actor is authorized to access.- Parameters:
nodeOwnershipDao- The DAO to use to fill in all available nodes for user-based actors, ornullto not fill in nodes.- Returns:
- The allowed node IDs.
- Throws:
AuthorizationException- if no node IDs are allowed or there is no actor- Since:
- 2.0
-
authorizedNodeIds
public static Long[] authorizedNodeIds(org.springframework.security.core.Authentication auth, SolarNodeOwnershipDao nodeOwnershipDao) Get all node IDs the given authentication is authorized to access.- Parameters:
auth- the authenticationnodeOwnershipDao- the DAO to use to fill in all available nodes for user-based actors, ornullto not fill in nodes- Returns:
- the allowed node IDs
- Throws:
AuthorizationException- if no node IDs are allowed or there is no actor- Since:
- 2.1
-
getActiveSecurityPolicy
Get aSecurityPolicyfor the active user, if available.- Returns:
- The active user's policy, or
null. - Since:
- 2.2
-