Class SecurityConfiguration
java.lang.Object
de.gustavblass.fsu.fmi.roombooking.configuration.SecurityConfiguration
@Configuration
@EnableWebSecurity
@EnableMethodSecurity
public class SecurityConfiguration
extends Object
Configures authentication and authorisation for the Spring application.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanWhether local registrations are enabled.private final @Nullable StringSeeAbstractContextSource.setBase(String).private final @Nullable StringThe URL of the LDAP server, including the matching protocol.private final @Nullable StringSeeAbstractLdapAuthenticator.setUserDnPatterns(java.lang.String[]).private @NonNull DurationThe timespan after which aVerificationTokenshall expire. -
Constructor Summary
ConstructorsConstructorDescriptionSecurityConfiguration(@Nullable String ldapUrl, @Nullable String ldapBase, @Nullable String userDnPattern, @Nullable String enableLocalRegistration, @Nullable Duration verificationTokenValidity) Constructs a newSecurityConfiguration. -
Method Summary
Modifier and TypeMethodDescription@NonNull org.springframework.security.crypto.argon2.Argon2PasswordEncoderCreates a new Argon2PasswordEncoder with Spring's defaults in order to hash the users' login credentials so that they're protected from being stolen by unauthorised people accessing the database.@NonNull org.springframework.security.authentication.AuthenticationEventPublisherauthenticationEventPublisher(@NonNull org.springframework.context.ApplicationEventPublisher applicationEventPublisher) Creates a newAuthenticationEventPublisher.@NonNull org.springframework.security.authentication.AuthenticationManagerauthenticationManager(@NonNull org.springframework.security.core.userdetails.UserDetailsService userDetailsService, @NonNull org.springframework.security.crypto.password.PasswordEncoder passwordEncoder, @NonNull org.springframework.security.authentication.AuthenticationEventPublisher authenticationEventPublisher, @Nullable org.springframework.security.ldap.authentication.LdapAuthenticationProvider ldapAuthenticationProvider) Creates a newDaoAuthenticationProviderusing theUserDetailsServiceand anargon2PasswordEncoder().@NonNull org.springframework.security.web.authentication.AuthenticationSuccessHandler@Nullable org.springframework.security.ldap.authentication.BindAuthenticatorCreates the authentication strategy for theldapAuthenticationProvider(UserDetailsService, BindAuthenticator).@Nullable org.springframework.security.ldap.authentication.LdapAuthenticationProviderldapAuthenticationProvider(@NonNull org.springframework.security.core.userdetails.UserDetailsService userDetailsService, @Nullable org.springframework.security.ldap.authentication.BindAuthenticator bindAuthenticator) Creates a newLdapAuthenticationProviderbased on the givenBindAuthenticator.(package private) static @NonNull org.springframework.security.access.expression.method.MethodSecurityExpressionHandlermethodSecurityExpressionHandler(@NonNull org.springframework.security.access.hierarchicalroles.RoleHierarchy roleHierarchy) Creates a newDefaultMethodSecurityExpressionHandlerfor theroleHierarchy().@NonNull RegistrationModeDetermines theRegistrationModeconfigured in the application properties.(package private) static @NonNull org.springframework.security.access.hierarchicalroles.RoleHierarchySpecifies aRoleHierarchywhere every role implicitly includes all lower roles.@NonNull org.springframework.security.web.SecurityFilterChainsecurityFilterChain(@NonNull org.springframework.security.config.annotation.web.builders.HttpSecurity http, @NonNull org.springframework.security.authentication.AuthenticationManager authenticationManager, @NonNull RegistrationMode registrationMode, @NonNull org.springframework.security.web.authentication.AuthenticationSuccessHandler authenticationSuccessHandler) Sets up basic user-password authentication for all paths except for the following ones:@NonNull Duration
-
Field Details
-
ldapUrl
The URL of the LDAP server, including the matching protocol. -
ldapBase
SeeAbstractContextSource.setBase(String). -
userDnPattern
SeeAbstractLdapAuthenticator.setUserDnPatterns(java.lang.String[]). Only one is expected. -
enableLocalRegistration
private boolean enableLocalRegistrationWhether local registrations are enabled. If true, LDAP registrations can still be enabled (i.e. that setting is not affected by this setting). -
verificationTokenValidity
The timespan after which aVerificationTokenshall expire.
-
-
Constructor Details
-
SecurityConfiguration
public SecurityConfiguration(@Nullable @Value("${spring.ldap.urls:}") @Nullable String ldapUrl, @Nullable @Value("${spring.ldap.base:}") @Nullable String ldapBase, @Nullable @Value("${spring.ldap.userDnPattern:}") @Nullable String userDnPattern, @Nullable @Value("${application.registration.local:}") @Nullable String enableLocalRegistration, @Nullable @Value("${application.verificationToken.validity:}") @Nullable Duration verificationTokenValidity) Constructs a newSecurityConfiguration.- Parameters:
ldapUrl- TheldapUrl.ldapBase- TheldapBase.userDnPattern- TheuserDnPattern.enableLocalRegistration- The value forenableLocalRegistration.verificationTokenValidity- TheverificationTokenValidity.
-
-
Method Details
-
registrationMode
Determines theRegistrationModeconfigured in the application properties.- Returns:
- The registration mode configured.
-
securityFilterChain
@Bean @DependsOn({"authenticationManager","registrationMode","authenticationSuccessHandler"}) @NonNull public @NonNull org.springframework.security.web.SecurityFilterChain securityFilterChain(@NonNull @NonNull org.springframework.security.config.annotation.web.builders.HttpSecurity http, @NonNull @NonNull org.springframework.security.authentication.AuthenticationManager authenticationManager, @NonNull @NonNull RegistrationMode registrationMode, @NonNull @NonNull org.springframework.security.web.authentication.AuthenticationSuccessHandler authenticationSuccessHandler) throws Exception Sets up basic user-password authentication for all paths except for the following ones:
- assets:
/css/**,/js/**,/images/** /login,/fehler/**,/error/**,/logout,//räume/**,en/rooms/**,/api/*/rooms/**/swagger-ui/**
Requires authority
ActorAuthority.FACULTY_ADMINISTRATIONor higher for/admin/**,/en/admin/**and/api/*/admin/**.- Parameters:
http- Used to configure the security filter chain.authenticationManager- Used to validate log-ins.- Returns:
- The
SecurityFilterChainrepresenting the above authentication and authorisation rules. - Throws:
Exception- SeeHttpSecurity.authorizeHttpRequests().
- assets:
-
authenticationSuccessHandler
@Bean("authenticationSuccessHandler") @NonNull public @NonNull org.springframework.security.web.authentication.AuthenticationSuccessHandler authenticationSuccessHandler()- Returns:
- A new
SessionAndSavedRequestAwareAuthenticationSuccessHandler.
-
authenticationManager
@Bean("authenticationManager") @DependsOn("ldap") @NonNull public @NonNull org.springframework.security.authentication.AuthenticationManager authenticationManager(@NonNull @NonNull org.springframework.security.core.userdetails.UserDetailsService userDetailsService, @NonNull @NonNull org.springframework.security.crypto.password.PasswordEncoder passwordEncoder, @NonNull @NonNull org.springframework.security.authentication.AuthenticationEventPublisher authenticationEventPublisher, @Nullable @Nullable org.springframework.security.ldap.authentication.LdapAuthenticationProvider ldapAuthenticationProvider) Creates a newDaoAuthenticationProviderusing theUserDetailsServiceand anargon2PasswordEncoder().- Parameters:
userDetailsService- The UserDetailsService used for authentication.passwordEncoder- ThePasswordEncoderfor hashing.authenticationEventPublisher- Configured in the newProviderManager.ldapAuthenticationProvider- If provided, will be passed to theAuthenticationManagerthat is created.- Returns:
- The
AuthenticationProviderused for authenticating the users in the UserDetailsService.
-
ldapAuthenticationProvider
@Bean("ldap") @DependsOn("bindAuthenticator") @Contract("_, null -> null") @Nullable public @Nullable org.springframework.security.ldap.authentication.LdapAuthenticationProvider ldapAuthenticationProvider(@NonNull @NonNull org.springframework.security.core.userdetails.UserDetailsService userDetailsService, @Nullable @Nullable org.springframework.security.ldap.authentication.BindAuthenticator bindAuthenticator) Creates a newLdapAuthenticationProviderbased on the givenBindAuthenticator. If it is null, thennullis returned.- Parameters:
userDetailsService- Used to compare the LDAP server's user data to the user data in the local database. If thisUserDetailsServiceis not anActorService, then no LdapAuthenticationProvider is created.bindAuthenticator- Does the actual authentication.- Returns:
- An LdapAuthenticationProvider that facilitates log-in verification with a specified LDAP server.
nullif thebindAuthenticatorisnull.
-
bindAuthenticator
@Bean("bindAuthenticator") @Nullable public @Nullable org.springframework.security.ldap.authentication.BindAuthenticator bindAuthenticator()Creates the authentication strategy for theldapAuthenticationProvider(UserDetailsService, BindAuthenticator). Uses theldapUrl,ldapBaseanduserDnPattern.- Returns:
- A new
BindAuthenticator.
-
argon2PasswordEncoder
@Bean @NonNull public @NonNull org.springframework.security.crypto.argon2.Argon2PasswordEncoder argon2PasswordEncoder()Creates a new Argon2PasswordEncoder with Spring's defaults in order to hash the users' login credentials so that they're protected from being stolen by unauthorised people accessing the database.- Returns:
- The new Argon2 password encoder.
-
verificationTokenValidity
- Returns:
- The
verificationTokenValidity.
-
authenticationEventPublisher
@Bean @NonNull public @NonNull org.springframework.security.authentication.AuthenticationEventPublisher authenticationEventPublisher(@NonNull @NonNull org.springframework.context.ApplicationEventPublisher applicationEventPublisher) Creates a newAuthenticationEventPublisher.- Parameters:
applicationEventPublisher- TheApplicationEventPublisherused by the new DefaultAuthenticationEventPublisher.- Returns:
- A new
DefaultAuthenticationEventPublisher.
-
roleHierarchy
@Bean @NonNull static @NonNull org.springframework.security.access.hierarchicalroles.RoleHierarchy roleHierarchy()Specifies a
RoleHierarchywhere every role implicitly includes all lower roles. Sorted by descending priority:- Returns:
- The role hierarchy as stated above.
-
methodSecurityExpressionHandler
@Bean @NonNull static @NonNull org.springframework.security.access.expression.method.MethodSecurityExpressionHandler methodSecurityExpressionHandler(@NonNull @NonNull org.springframework.security.access.hierarchicalroles.RoleHierarchy roleHierarchy) Creates a newDefaultMethodSecurityExpressionHandlerfor theroleHierarchy().- Parameters:
roleHierarchy- The role hierarchy that shall be used for authorisation.- Returns:
- The new DefaultMethodSecurityExpressionHandler.
-