|
|
@ -2,37 +2,79 @@ package com.example.demo.config;
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
|
|
|
import org.springframework.http.HttpMethod;
|
|
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
|
|
|
|
|
|
import org.springframework.security.authentication.AuthenticationProvider;
|
|
|
|
|
|
|
|
import org.springframework.security.authentication.ProviderManager;
|
|
|
|
|
|
|
|
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
|
|
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
|
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
|
|
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
|
|
|
import org.springframework.security.config.http.SessionCreationPolicy;
|
|
|
|
import org.springframework.security.config.http.SessionCreationPolicy;
|
|
|
|
|
|
|
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
|
|
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
|
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
|
import org.springframework.security.web.SecurityFilterChain;
|
|
|
|
import org.springframework.security.web.SecurityFilterChain;
|
|
|
|
|
|
|
|
import org.springframework.web.cors.CorsConfiguration;
|
|
|
|
|
|
|
|
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration
|
|
|
|
@Configuration
|
|
|
|
@EnableWebSecurity
|
|
|
|
@EnableWebSecurity
|
|
|
|
public class SecurityConfig {
|
|
|
|
public class SecurityConfig {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final UserDetailsService userDetailsService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public SecurityConfig(UserDetailsService userDetailsService) {
|
|
|
|
|
|
|
|
this.userDetailsService = userDetailsService;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
|
|
|
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
|
|
|
http
|
|
|
|
http
|
|
|
|
.cors().and()
|
|
|
|
.cors(cors -> cors.configurationSource(corsConfigurationSource()))
|
|
|
|
.csrf().disable()
|
|
|
|
|
|
|
|
.authorizeHttpRequests(authorize -> authorize
|
|
|
|
.authorizeHttpRequests(authorize -> authorize
|
|
|
|
.requestMatchers("/api/users/register", "/api/users/login").permitAll() // 允许注册和登录请求
|
|
|
|
.requestMatchers("/api/users/register", "/api/users/login").permitAll()
|
|
|
|
.anyRequest().authenticated() // 其他所有请求都需要认证
|
|
|
|
.anyRequest().authenticated()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.sessionManagement(session -> session
|
|
|
|
.sessionManagement(session -> session
|
|
|
|
.sessionCreationPolicy(SessionCreationPolicy.STATELESS) // 如果不使用会话状态(例如使用JWT),可以设置为STATELESS
|
|
|
|
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
|
|
|
|
);
|
|
|
|
)
|
|
|
|
|
|
|
|
.csrf().disable();
|
|
|
|
|
|
|
|
|
|
|
|
return http.build();
|
|
|
|
return http.build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private UrlBasedCorsConfigurationSource corsConfigurationSource() {
|
|
|
|
|
|
|
|
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
|
|
|
|
|
|
|
CorsConfiguration config = new CorsConfiguration();
|
|
|
|
|
|
|
|
config.setAllowCredentials(true);
|
|
|
|
|
|
|
|
config.addAllowedOrigin("http://localhost:5173");
|
|
|
|
|
|
|
|
config.addAllowedHeader("*");
|
|
|
|
|
|
|
|
config.addAllowedMethod(HttpMethod.GET);
|
|
|
|
|
|
|
|
config.addAllowedMethod(HttpMethod.POST);
|
|
|
|
|
|
|
|
config.addAllowedMethod(HttpMethod.PUT);
|
|
|
|
|
|
|
|
config.addAllowedMethod(HttpMethod.DELETE);
|
|
|
|
|
|
|
|
config.addAllowedMethod(HttpMethod.OPTIONS);
|
|
|
|
|
|
|
|
source.registerCorsConfiguration("/**", config);
|
|
|
|
|
|
|
|
return source;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
|
|
|
public PasswordEncoder passwordEncoder() {
|
|
|
|
|
|
|
|
return new BCryptPasswordEncoder();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
|
|
|
public AuthenticationProvider authenticationProvider() {
|
|
|
|
|
|
|
|
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
|
|
|
|
|
|
|
|
authProvider.setUserDetailsService(userDetailsService);
|
|
|
|
|
|
|
|
authProvider.setPasswordEncoder(passwordEncoder());
|
|
|
|
|
|
|
|
return authProvider;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
public AuthenticationManager authenticationManager(org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration authConfig) throws Exception {
|
|
|
|
public AuthenticationManager authenticationManager(AuthenticationProvider authenticationProvider) {
|
|
|
|
return authConfig.getAuthenticationManager();
|
|
|
|
return new ProviderManager(List.of(authenticationProvider));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|