|
4 | 4 | import comp9323.group12.backend.component.auth.RestAuthenticationFailureHandler;
|
5 | 5 | import comp9323.group12.backend.component.auth.RestAuthenticationSuccessHandler;
|
6 | 6 | import org.springframework.beans.factory.annotation.Autowired;
|
| 7 | +import org.springframework.context.annotation.Bean; |
7 | 8 | import org.springframework.http.HttpMethod;
|
| 9 | +import org.springframework.security.config.Customizer; |
8 | 10 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
9 | 11 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
10 | 12 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
11 | 13 | import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
|
12 | 14 | import org.springframework.web.bind.annotation.CrossOrigin;
|
| 15 | +import org.springframework.web.cors.CorsConfiguration; |
| 16 | +import org.springframework.web.cors.CorsConfigurationSource; |
| 17 | +import org.springframework.web.cors.UrlBasedCorsConfigurationSource; |
| 18 | + |
| 19 | +import java.util.Arrays; |
13 | 20 |
|
14 | 21 |
|
15 | 22 | @EnableWebSecurity
|
@@ -42,7 +49,18 @@ protected void configure(HttpSecurity http) throws Exception {
|
42 | 49 | .authorizeRequests()
|
43 | 50 | .antMatchers( "/api/unauthorized", "/api/signup", "/api/login").permitAll()
|
44 | 51 | .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
|
45 |
| - .anyRequest().authenticated(); |
| 52 | + .anyRequest().authenticated() |
| 53 | + .and().cors(Customizer.withDefaults()); |
| 54 | + } |
| 55 | + |
| 56 | + @Bean |
| 57 | + CorsConfigurationSource corsConfigurationSource() { |
| 58 | + CorsConfiguration configuration = new CorsConfiguration(); |
| 59 | + configuration.setAllowedOrigins(Arrays.asList("*")); |
| 60 | + configuration.setAllowedMethods(Arrays.asList("GET","POST", "OPTIONS", "PUT", "DELETE")); |
| 61 | + UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); |
| 62 | + source.registerCorsConfiguration("/**", configuration); |
| 63 | + return source; |
46 | 64 | }
|
47 | 65 | }
|
48 | 66 |
|
0 commit comments