Class SecurityConfig
This class configures Spring Security settings including authentication, authorization, JWT token filtering, password encoding, and endpoint access rules. It establishes a stateless authentication system using JWT tokens and defines which API endpoints are public and which require authentication.
The configuration enables method-level security annotations, disables CSRF protection (as we use JWT tokens), and sets up the security filter chain.
- Since:
- 2025-05-14
-
Constructor Summary
ConstructorsConstructorDescriptionSecurityConfig(UserDetailsServiceImpl userDetailsService, JwtAuthenticationEntryPoint unauthorizedHandler) Constructs a new SecurityConfig with required dependencies. -
Method Summary
Modifier and TypeMethodDescriptionCreates a JWT authentication token filter bean.org.springframework.security.authentication.AuthenticationManagerauthenticationManager(org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration authConfig) Creates an authentication manager bean from the authentication configuration.org.springframework.security.authentication.dao.DaoAuthenticationProviderCreates an authentication provider bean that uses the UserDetailsService and password encoder for authenticating users.org.springframework.security.web.SecurityFilterChainfilterChain(org.springframework.security.config.annotation.web.builders.HttpSecurity http) Configures the security filter chain with authorization rules.org.springframework.security.crypto.password.PasswordEncoderCreates a password encoder bean for securely hashing passwords.
-
Constructor Details
-
SecurityConfig
public SecurityConfig(UserDetailsServiceImpl userDetailsService, JwtAuthenticationEntryPoint unauthorizedHandler) Constructs a new SecurityConfig with required dependencies.- Parameters:
userDetailsService- The service for loading user-specific dataunauthorizedHandler- The entry point for handling unauthorized access attempts
-
-
Method Details
-
authenticationJwtTokenFilter
Creates a JWT authentication token filter bean.This filter intercepts and processes JWT tokens from incoming requests, validating them and setting up the security context if the token is valid.
- Returns:
- A new JWT authentication token filter
-
passwordEncoder
Creates a password encoder bean for securely hashing passwords.This encoder is used for both validating existing passwords during authentication and encoding new passwords during user registration.
- Returns:
- A BCrypt password encoder instance
-
authenticationProvider
@Bean public org.springframework.security.authentication.dao.DaoAuthenticationProvider authenticationProvider()Creates an authentication provider bean that uses the UserDetailsService and password encoder for authenticating users.- Returns:
- The configured authentication provider
-
authenticationManager
@Bean public org.springframework.security.authentication.AuthenticationManager authenticationManager(org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration authConfig) throws Exception Creates an authentication manager bean from the authentication configuration.- Parameters:
authConfig- The authentication configuration- Returns:
- The authentication manager
- Throws:
Exception- If an error occurs when creating the authentication manager
-
filterChain
@Bean public org.springframework.security.web.SecurityFilterChain filterChain(org.springframework.security.config.annotation.web.builders.HttpSecurity http) throws Exception Configures the security filter chain with authorization rules.This method defines which endpoints are accessible without authentication and which require authentication. It configures JWT-based stateless authentication and sets up exception handling for unauthorized access.
- Parameters:
http- The HTTP security configuration object- Returns:
- The configured security filter chain
- Throws:
Exception- If an error occurs during configuration
-