Class AuthenticationContext
It allows to access authenticated user information and to initiate the logout
process.
An instance of this class is available for injection as bean in view and
layout classes. The class is not Serializable, so potential
referencing fields in Vaadin views should be defined transient.
- Since:
- 23.3
- Author:
- Vaadin Ltd
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidapplySecurityConfiguration(org.springframework.security.config.annotation.web.builders.HttpSecurity httpSecurity, AuthenticationContext authCtx) Augments the givenAuthenticationContextwith Spring Security.<U> Optional<U> getAuthenticatedUser(Class<U> userType) Gets anOptionalwith an instance of the current user if it has been authenticated, or empty if the user is not authenticated.Collection<? extends org.springframework.security.core.GrantedAuthority> Gets the authorities granted to the current authenticated user.Gets the roles granted to the current authenticated user.Gets anOptionalcontaining the authenticated principal name, or an empty optional if the user is not authenticated.booleanhasAllAuthorities(String... authorities) Checks whether the current authenticated user has all the given authorities.booleanhasAllAuthorities(Collection<String> authorities) Checks whether the current authenticated user has all the given authorities.booleanhasAllRoles(String... roles) Checks whether the current authenticated user has all the given roles.booleanhasAllRoles(Collection<String> roles) Checks whether the current authenticated user has all the given roles.booleanhasAnyAuthority(String... authorities) Checks whether the current authenticated user has any of the given authorities.booleanhasAnyAuthority(Collection<String> authorities) Checks whether the current authenticated user has any of the given authorities.booleanhasAnyRole(String... roles) Checks whether the current authenticated user has any of the given roles.booleanhasAnyRole(Collection<String> roles) Checks whether the current authenticated user has any of the given roles.booleanhasAuthority(String authority) Checks whether the current authenticated user has the given authority.booleanChecks whether the current authenticated user has the given role.booleanIndicates whether a user is currently authenticated.voidlogout()Initiates the logout process of the current authenticated user by invalidating the local session and then notifyingLogoutHandler.
-
Constructor Details
-
AuthenticationContext
public AuthenticationContext()
-
-
Method Details
-
getAuthenticatedUser
Gets anOptionalwith an instance of the current user if it has been authenticated, or empty if the user is not authenticated. Anonymous users are considered not authenticated.- Type Parameters:
U- the type parameter of the expected user instance- Parameters:
userType- the type of the expected user instance- Returns:
- an
Optionalwith the current authenticated user, or empty if none available - Throws:
ClassCastException- if the current user instance does not match the givenuserType.
-
getPrincipalName
Gets anOptionalcontaining the authenticated principal name, or an empty optional if the user is not authenticated. The principal name usually refers to a username or an identifier that can be used to retrieve additional information for the authenticated user. Anonymous users are considered not authenticated.- Returns:
- an
Optionalcontaining the authenticated principal name or an empty optional if not available.
-
isAuthenticated
public boolean isAuthenticated()Indicates whether a user is currently authenticated. Anonymous users are considered not authenticated.- Returns:
- true if a user is currently authenticated, otherwise false
-
logout
public void logout()Initiates the logout process of the current authenticated user by invalidating the local session and then notifyingLogoutHandler. -
getGrantedAuthorities
public Collection<? extends org.springframework.security.core.GrantedAuthority> getGrantedAuthorities()Gets the authorities granted to the current authenticated user.- Returns:
- an unmodifiable collection of
GrantedAuthoritys or an empty collection if there is no authenticated user.
-
getGrantedRoles
Gets the roles granted to the current authenticated user.- Returns:
- an unmodifiable collection of role names (without the role prefix) or an empty collection if there is no authenticated user.
-
hasRole
Checks whether the current authenticated user has the given role.The role must be provided without the role prefix, for example
hasRole("USER")instead ofhasRole("ROLE_USER").- Parameters:
role- the role to check, without the role prefix.- Returns:
- true if the user holds the given role, otherwise false.
-
hasAnyRole
Checks whether the current authenticated user has any of the given roles.Roles must be provided without the role prefix, for example
hasAnyRole(Set.of("USER", "ADMIN"))instead ofhasAnyRole(Set.of("ROLE_USER", "ROLE_ADMIN")).- Parameters:
roles- a collection containing at least one role, without the role prefix.- Returns:
- true if the user holds at least one of the given roles, otherwise false.
- Throws:
IllegalArgumentException- if the given collection is empty.
-
hasAnyRole
Checks whether the current authenticated user has any of the given roles.Roles must be provided without the role prefix, for example
hasAnyRole("USER", "ADMIN")instead ofhasAnyRole("ROLE_USER", "ROLE_ADMIN").- Parameters:
roles- an array containing at least one role, without the role prefix.- Returns:
- true if the user holds at least one of the given roles, otherwise false.
- Throws:
IllegalArgumentException- if the given array is empty.
-
hasAllRoles
Checks whether the current authenticated user has all the given roles.Roles must be provided without the role prefix, for example
hasAllRoles(Set.of("USER", "ADMIN"))instead ofhasAllRoles(Set.of("ROLE_USER", "ROLE_ADMIN")).- Parameters:
roles- a collection containing at least one role, without the role prefix.- Returns:
- true if the user holds all the given roles, otherwise false.
- Throws:
IllegalArgumentException- if the given collection is empty.
-
hasAllRoles
Checks whether the current authenticated user has all the given roles.Roles must be provided without the role prefix, for example
hasAllRoles("USER", "ADMIN")instead ofhasAllRoles("ROLE_USER", "ROLE_ADMIN").- Parameters:
roles- an array containing at least one role, without the role prefix.- Returns:
- true if the user holds all the given roles, otherwise false.
- Throws:
IllegalArgumentException- if the given array is empty.
-
hasAuthority
Checks whether the current authenticated user has the given authority.- Parameters:
authority- the authority to check.- Returns:
- true if the user holds the given authority, otherwise false.
-
hasAnyAuthority
Checks whether the current authenticated user has any of the given authorities.- Parameters:
authorities- a collection containing at least one authority.- Returns:
- true if the user holds at least one of the given authorities, otherwise false.
- Throws:
IllegalArgumentException- if the given collection is empty.
-
hasAnyAuthority
Checks whether the current authenticated user has any of the given authorities.- Parameters:
authorities- an array containing at least one authority.- Returns:
- true if the user holds at least one of the given authorities, otherwise false.
- Throws:
IllegalArgumentException- if the given array is empty.
-
hasAllAuthorities
Checks whether the current authenticated user has all the given authorities.- Parameters:
authorities- a collection containing at least one authority.- Returns:
- true if the user holds all the given authorities, otherwise false.
- Throws:
IllegalArgumentException- if the given collection is empty.
-
hasAllAuthorities
Checks whether the current authenticated user has all the given authorities.- Parameters:
authorities- an array containing at least one authority.- Returns:
- true if the user holds all the given authorities, otherwise false.
- Throws:
IllegalArgumentException- if the given array is empty.
-
applySecurityConfiguration
public static void applySecurityConfiguration(org.springframework.security.config.annotation.web.builders.HttpSecurity httpSecurity, AuthenticationContext authCtx) Augments the givenAuthenticationContextwith Spring Security. This method can be used to configure theAuthenticationContextwhenVaadinSecurityConfigureris not used to set up Spring Security.- Parameters:
httpSecurity- SpringHttpSecurityfor security configurationauthCtx- The authentication context of the application.
-