Class CoreSecurityConfig
java.lang.Object
tech.lastbox.lastshield.security.core.CoreSecurityConfig
The
CoreSecurityConfig class configures the security settings for the web application,
including authorization, authentication, and Cross-Site Request Forgery (CSRF) protection.
It uses Spring Security to define custom security filter chains and manages path-based access control
using RouteAuthority objects.
This class is responsible for setting up the security filter chain, including the handling of custom CORS configurations, CSRF protection settings, and adding custom authorities for route access.
-
Constructor Summary
ConstructorsConstructorDescriptionCoreSecurityConfig(CorsConfig corsConfig, SecurityUtil securityUtil, SecurityFilter securityFilter) Constructs a newCoreSecurityConfiginstance with the required dependencies for CORS configuration, security utilities, and a custom security filter. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAuthority(RouteAuthority routeAuthority) Adds aRouteAuthorityto the list of authorities.org.springframework.security.web.SecurityFilterChaindefaultSecurityFilterChain(org.springframework.security.config.annotation.web.builders.HttpSecurity http) Configures the defaultSecurityFilterChainwith CORS, CSRF protection, and authority-based access control.voidisCalled()Marks the security configuration as initialized.voidsetCsrfProtection(boolean csrfProtection) Enables or disables CSRF protection for the application.
-
Constructor Details
-
CoreSecurityConfig
public CoreSecurityConfig(CorsConfig corsConfig, SecurityUtil securityUtil, SecurityFilter securityFilter) Constructs a newCoreSecurityConfiginstance with the required dependencies for CORS configuration, security utilities, and a custom security filter. This constructor uses Spring's Dependency Injection mechanism to inject the necessary beans into the class.- Parameters:
corsConfig- theCorsConfigobject used to configure Cross-Origin Resource Sharing (CORS) settings.securityUtil- theSecurityUtilutility used for various security-related operations, such as user repository handling.securityFilter- theSecurityFilterthat is added to the security filter chain to handle custom authentication and authorization.
-
-
Method Details
-
defaultSecurityFilterChain
@Bean public org.springframework.security.web.SecurityFilterChain defaultSecurityFilterChain(org.springframework.security.config.annotation.web.builders.HttpSecurity http) throws Exception Configures the defaultSecurityFilterChainwith CORS, CSRF protection, and authority-based access control.If the
build()method was not previously called, an exception will be thrown. This method sets up the security filter chain by adding theSecurityFilterand configuring paths and roles.- Parameters:
http- theHttpSecurityobject used to configure security settings.- Returns:
- the configured
SecurityFilterChain. - Throws:
Exception- if there is any error while configuring the security filter chain.
-
addAuthority
Adds aRouteAuthorityto the list of authorities. If the providedRouteAuthoritydoes not have any associated roles, the path of the authority is added to the list of paths that should not be filtered by the advanced filter using theAdvancedFilterChecker.addShoudNotFilterPath(String)method.If the advanced filter is not currently enabled (
AdvancedFilterChecker.isAdvancedFiltered()returnsfalse), this method will enable it by callingsetAdvancedFilter()and configure theSecurityFilterwith the appropriate user repository class usingSecurityUtil.getUserRepositoryClass().- Parameters:
routeAuthority- theRouteAuthorityobject representing the authority to be added.
-
setCsrfProtection
public void setCsrfProtection(boolean csrfProtection) Enables or disables CSRF protection for the application.- Parameters:
csrfProtection-trueto enable CSRF protection,falseto disable it.
-
isCalled
public void isCalled()Marks the security configuration as initialized. This should be called after the configuration is properly set up.
-