package com.luojia_channel.modules.user.Config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
public class SecurityConfig {
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
                .csrf(csrf -> csrf.disable()) //禁用CSRF保护
                .cors(cors -> cors.disable()) //禁用CORS测试，似乎需要，没有详细测试过
                .authorizeHttpRequests(auth -> auth
                        .requestMatchers("/user/login",
                                "/user/register",
                                "/user/hello").permitAll() //允许所有用户访问登录和注册接口
                        .anyRequest().authenticated() //其他请求需要认证
                );
        return http.build();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
