Skip to content

Commit

Permalink
[conluzweb-36] Configured CORS
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorKhan committed Jun 26, 2024
1 parent 53105ec commit 697f1bd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.lucoenergia.conluz.infrastructure.shared.security.auth.JwtAuthenticationFilter;
import org.lucoenergia.conluz.infrastructure.shared.web.error.ConluzAccessDeniedHandler;
import org.lucoenergia.conluz.infrastructure.shared.web.error.ConluzAuthenticationEntryPoint;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationProvider;
Expand All @@ -21,11 +22,15 @@
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Arrays;
import java.util.List;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig {

@Value("#{'${conluz.allowed.origins}'.split(',')}")
private List<String> allowedOrigins;

private final JwtAuthenticationFilter jwtAuthenticationFilter;
private final AuthenticationProvider authenticationProvider;
private final ObjectMapper objectMapper;
Expand Down Expand Up @@ -69,9 +74,10 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("*"));
configuration.setAllowedMethods(Arrays.asList("*"));
configuration.setAllowedOrigins(allowedOrigins);
configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE, OPTIONS, HEAD, TRACE"));
configuration.setAllowedHeaders(Arrays.asList("*"));
configuration.setAllowCredentials(true);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,7 @@ server.ssl.key-store=classpath:keystore/conluz.p12
server.ssl.key-store-password=changeit
# The alias mapped to the certificate
server.ssl.key-alias=conluz

## CORS configuration
# We can specify a list of origins separated by comas. Example: http://localhost:3000,http://localhost:4000,http://localhost:5000
conluz.allowed.origins=http://localhost:3001
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.lucoenergia.conluz.infrastructure.shared.BaseControllerTest;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

class WebSecurityConfigTest extends BaseControllerTest {
Expand All @@ -23,4 +22,4 @@ void testSecurityFilterChainForHealth() throws Exception {
void testSecurityFilterChainForInfo() throws Exception {
mockMvc.perform(get("/actuator/info")).andExpect(status().isOk());
}
}
}

0 comments on commit 697f1bd

Please sign in to comment.