Skip to content

Commit 9df529a

Browse files
committed
attempt fix cors
1 parent 7c65825 commit 9df529a

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

backend/src/main/java/comp9323/group12/backend/config/SecurityConfig.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@
44
import comp9323.group12.backend.component.auth.RestAuthenticationFailureHandler;
55
import comp9323.group12.backend.component.auth.RestAuthenticationSuccessHandler;
66
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.context.annotation.Bean;
78
import org.springframework.http.HttpMethod;
9+
import org.springframework.security.config.Customizer;
810
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
911
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
1012
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
1113
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
1214
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;
1320

1421

1522
@EnableWebSecurity
@@ -42,7 +49,18 @@ protected void configure(HttpSecurity http) throws Exception {
4249
.authorizeRequests()
4350
.antMatchers( "/api/unauthorized", "/api/signup", "/api/login").permitAll()
4451
.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;
4664
}
4765
}
4866

0 commit comments

Comments
 (0)