Package com.vaadin.flow.spring.security
Class AuthenticationContext
java.lang.Object
com.vaadin.flow.spring.security.AuthenticationContext
The authentication context of the application.
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
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
applySecurityConfiguration
(org.springframework.security.config.annotation.web.builders.HttpSecurity httpSecurity, AuthenticationContext authCtx) Augments the givenAuthenticationContext
with Spring Security.<U> Optional<U>
getAuthenticatedUser
(Class<U> userType) Gets anOptional
with an instance of the current user if it has been authenticated, or empty if the user is not authenticated.Gets anOptional
containing the authenticated principal name, or an empty optional if the user is not authenticated.boolean
Indicates whether a user is currently authenticated.void
logout()
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 anOptional
with 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
Optional
with the current authenticated user, or empty if none available - Throws:
ClassCastException
- if the current user instance does not match the givenuserType
.
-
getPrincipalName
Gets anOptional
containing 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
Optional
containing 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
. -
applySecurityConfiguration
public static void applySecurityConfiguration(org.springframework.security.config.annotation.web.builders.HttpSecurity httpSecurity, AuthenticationContext authCtx) Augments the givenAuthenticationContext
with Spring Security. This method can be used to configure theAuthenticationContext
whenVaadinWebSecurity
is not used to set up Spring Security.- Parameters:
httpSecurity
- SpringHttpSecurity
for security configurationauthCtx
- The authentication context of the application.
-