2025-05-08 15:52:51 +09:00
|
|
|
package co.jp.app.config.security;
|
|
|
|
|
|
|
|
import co.jp.app.config.security.filter.JwtAuthenticationFilter;
|
|
|
|
import co.jp.app.service.UserService;
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
|
|
import org.springframework.security.authentication.AuthenticationProvider;
|
|
|
|
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
|
|
|
|
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
|
|
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
|
|
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
|
|
|
|
import org.springframework.security.config.http.SessionCreationPolicy;
|
|
|
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
|
import org.springframework.security.web.SecurityFilterChain;
|
|
|
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
|
|
|
|
|
|
|
@Configuration
|
|
|
|
public class SecurityConfig {
|
|
|
|
|
2025-05-09 15:35:13 +09:00
|
|
|
private final JwtAuthenticationFilter jwtAuthenticationFilter;
|
|
|
|
private final UserService userService;
|
2025-05-08 15:52:51 +09:00
|
|
|
|
2025-05-09 15:35:13 +09:00
|
|
|
public SecurityConfig(JwtAuthenticationFilter jwtAuthenticationFilter, UserService userService) {
|
|
|
|
this.jwtAuthenticationFilter = jwtAuthenticationFilter;
|
|
|
|
this.userService = userService;
|
|
|
|
}
|
2025-05-08 15:52:51 +09:00
|
|
|
|
|
|
|
@Bean
|
|
|
|
public PasswordEncoder passwordEncoder() {
|
|
|
|
return new BCryptPasswordEncoder();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
public AuthenticationProvider authenticationProvider() {
|
|
|
|
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
|
|
|
|
authProvider.setUserDetailsService(userService);
|
|
|
|
authProvider.setPasswordEncoder(passwordEncoder());
|
|
|
|
return authProvider;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
|
|
|
|
return authenticationConfiguration.getAuthenticationManager();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
|
|
|
http.csrf(AbstractHttpConfigurer::disable)
|
|
|
|
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
|
|
|
.authorizeHttpRequests(auth -> auth
|
|
|
|
.requestMatchers("/api/user/login", "/api/user/register").permitAll()
|
|
|
|
.anyRequest().authenticated()
|
|
|
|
)
|
|
|
|
.authenticationProvider(authenticationProvider())
|
|
|
|
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
|
|
|
|
|
|
|
|
return http.build();
|
|
|
|
}
|
|
|
|
}
|