|
1 | 1 | package edu.bu.cs673.secondhand.config;
|
| 2 | +// |
| 3 | +//import org.springframework.context.annotation.Bean; |
| 4 | +//import org.springframework.context.annotation.Configuration; |
| 5 | +//import org.springframework.http.HttpMethod; |
| 6 | +//import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 7 | +//import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 8 | +//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
| 9 | +//import org.springframework.security.crypto.password.PasswordEncoder; |
| 10 | +//import org.springframework.security.web.SecurityFilterChain; |
| 11 | +// |
| 12 | +////@Configuration |
| 13 | +////@EnableWebSecurity |
| 14 | +//public class SecurityConfig { |
| 15 | +// |
| 16 | +// @Bean |
| 17 | +// public PasswordEncoder passwordEncoder() { |
| 18 | +// return new BCryptPasswordEncoder(); |
| 19 | +// } |
| 20 | +// |
| 21 | +// @Bean |
| 22 | +// public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { |
| 23 | +// http |
| 24 | +// .csrf().disable() |
| 25 | +// .authorizeHttpRequests((requests) -> requests |
| 26 | +// .requestMatchers(HttpMethod.GET, "/user/activate").permitAll() |
| 27 | +// .requestMatchers("/user/**").permitAll() |
| 28 | +// .anyRequest().authenticated() |
| 29 | +// ); |
| 30 | +// return http.build(); |
| 31 | +// } |
| 32 | +//} |
2 | 33 |
|
| 34 | +/*** |
| 35 | + Email: la1993@bu.edu |
| 36 | + DateTime: 10/18/24-14:24 |
| 37 | + *****/ |
3 | 38 | import org.springframework.context.annotation.Bean;
|
4 | 39 | import org.springframework.context.annotation.Configuration;
|
5 |
| -import org.springframework.http.HttpMethod; |
6 | 40 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
7 | 41 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
8 |
| -import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
9 |
| -import org.springframework.security.crypto.password.PasswordEncoder; |
10 | 42 | import org.springframework.security.web.SecurityFilterChain;
|
| 43 | +import org.springframework.web.cors.CorsConfiguration; |
| 44 | +import org.springframework.web.cors.UrlBasedCorsConfigurationSource; |
| 45 | +import org.springframework.web.filter.CorsFilter; |
11 | 46 |
|
12 |
| -//@Configuration |
13 |
| -//@EnableWebSecurity |
14 |
| -public class SecurityConfig { |
| 47 | +import java.util.List; |
15 | 48 |
|
16 |
| - @Bean |
17 |
| - public PasswordEncoder passwordEncoder() { |
18 |
| - return new BCryptPasswordEncoder(); |
19 |
| - } |
| 49 | +@Configuration |
| 50 | +@EnableWebSecurity |
| 51 | +public class SecurityConfig { |
20 | 52 |
|
21 | 53 | @Bean
|
22 | 54 | public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
23 | 55 | http
|
24 |
| - .csrf().disable() |
25 |
| - .authorizeHttpRequests((requests) -> requests |
26 |
| - .requestMatchers(HttpMethod.GET, "/user/activate").permitAll() |
27 |
| - .requestMatchers("/user/**").permitAll() |
28 |
| - .anyRequest().authenticated() |
29 |
| - ); |
| 56 | + .cors(cors -> cors.configurationSource(corsConfigurationSource())) |
| 57 | + .csrf(csrf -> csrf.disable()) |
| 58 | + .authorizeHttpRequests(authorize -> authorize |
| 59 | + .requestMatchers("/**").permitAll() |
| 60 | + ); |
| 61 | + |
30 | 62 | return http.build();
|
31 | 63 | }
|
| 64 | + |
| 65 | + @Bean |
| 66 | + public CorsFilter corsFilter() { |
| 67 | + CorsConfiguration config = new CorsConfiguration(); |
| 68 | + config.setAllowedOrigins(List.of("http://localhost:8081")); |
| 69 | + config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS")); |
| 70 | + config.setAllowedHeaders(List.of("*")); |
| 71 | + config.setAllowCredentials(true); |
| 72 | + |
| 73 | + UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); |
| 74 | + source.registerCorsConfiguration("/**", config); |
| 75 | + return new CorsFilter(source); |
| 76 | + } |
| 77 | + |
| 78 | + private UrlBasedCorsConfigurationSource corsConfigurationSource() { |
| 79 | + CorsConfiguration config = new CorsConfiguration(); |
| 80 | + config.setAllowedOrigins(List.of("http://localhost:8081")); |
| 81 | + config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS")); |
| 82 | + config.setAllowedHeaders(List.of("*")); |
| 83 | + config.setAllowCredentials(true); |
| 84 | + UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); |
| 85 | + source.registerCorsConfiguration("/**", config); |
| 86 | + return source; |
| 87 | + } |
32 | 88 | }
|
| 89 | + |
| 90 | + |
0 commit comments