2025-05-08 15:52:51 +09:00
|
|
|
package co.jp.app.config.security;
|
|
|
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
2025-05-12 14:06:12 +09:00
|
|
|
import org.springframework.context.annotation.Lazy;
|
2025-05-08 15:52:51 +09:00
|
|
|
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;
|
2025-05-12 14:06:12 +09:00
|
|
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
2025-05-08 15:52:51 +09:00
|
|
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
|
import org.springframework.security.web.SecurityFilterChain;
|
2025-05-14 14:17:03 +09:00
|
|
|
|
|
|
|
import co.jp.app.config.security.filter.JwtAuthenticationFilter;
|
2025-05-08 15:52:51 +09:00
|
|
|
|
|
|
|
@Configuration
|
|
|
|
public class SecurityConfig {
|
|
|
|
|
2025-05-12 16:39:08 +09:00
|
|
|
//private final JwtAuthenticationFilter jwtAuthenticationFilter;
|
2025-05-12 14:06:12 +09:00
|
|
|
private final UserDetailsService userDetailsService;
|
2025-05-08 15:52:51 +09:00
|
|
|
|
2025-05-12 14:06:12 +09:00
|
|
|
public SecurityConfig(@Lazy JwtAuthenticationFilter jwtAuthenticationFilter, @Lazy UserDetailsService userDetailsService) {
|
2025-05-12 16:39:08 +09:00
|
|
|
//this.jwtAuthenticationFilter = jwtAuthenticationFilter;
|
2025-05-12 14:06:12 +09:00
|
|
|
this.userDetailsService = userDetailsService;
|
2025-05-09 15:35:13 +09:00
|
|
|
}
|
2025-05-08 15:52:51 +09:00
|
|
|
|
|
|
|
@Bean
|
|
|
|
public PasswordEncoder passwordEncoder() {
|
|
|
|
return new BCryptPasswordEncoder();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
public AuthenticationProvider authenticationProvider() {
|
|
|
|
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
|
2025-05-12 14:06:12 +09:00
|
|
|
authProvider.setUserDetailsService(userDetailsService);
|
2025-05-08 15:52:51 +09:00
|
|
|
authProvider.setPasswordEncoder(passwordEncoder());
|
|
|
|
return authProvider;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
|
|
|
|
return authenticationConfiguration.getAuthenticationManager();
|
|
|
|
}
|
|
|
|
|
2025-05-12 16:39:08 +09:00
|
|
|
// @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();
|
|
|
|
// }
|
|
|
|
|
2025-05-08 15:52:51 +09:00
|
|
|
@Bean
|
2025-05-12 16:39:08 +09:00
|
|
|
//暂时开放所有权限
|
2025-05-08 15:52:51 +09:00
|
|
|
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
2025-05-12 16:39:08 +09:00
|
|
|
|
2025-05-08 15:52:51 +09:00
|
|
|
http.csrf(AbstractHttpConfigurer::disable)
|
|
|
|
.authorizeHttpRequests(auth -> auth
|
2025-05-12 16:39:08 +09:00
|
|
|
.anyRequest().permitAll()
|
|
|
|
);
|
2025-05-08 15:52:51 +09:00
|
|
|
|
|
|
|
return http.build();
|
|
|
|
}
|
|
|
|
}
|