Skip to content

Commit

Permalink
fix:QualityGate
Browse files Browse the repository at this point in the history
Refactor CORS configuration for better readability.

Replaces inline CORS setup with a more structured approach by creating and returning an instance of `CorsConfiguration`. This improves the readability and maintainability of the code, aligning with best practices. No functional changes were introduced.
  • Loading branch information
k2works committed Dec 27, 2024
1 parent 5701dd4 commit 1f0c4b8
Showing 1 changed file with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ public class WebSecurityConfig {
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

@Bean
public AuthTokenFilter authenticationJwtTokenFilter() {
return new AuthTokenFilter();
}

@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.formLogin(login -> login
Expand All @@ -47,13 +50,14 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
).csrf(csrf -> csrf.ignoringRequestMatchers(PathRequest.toH2Console())
).csrf(csrf -> csrf.ignoringRequestMatchers("/api/**")
).cors(cors -> cors
.configurationSource(request -> new org.springframework.web.cors.CorsConfiguration() {{
setAllowedOriginPatterns(java.util.List.of("*"));
setAllowedMethods(java.util.List.of("GET", "POST", "PUT", "DELETE", "OPTIONS"));
setAllowedHeaders(java.util.List.of("*"));
setAllowCredentials(true);
}}
)
.configurationSource(request -> {
org.springframework.web.cors.CorsConfiguration corsConfiguration = new org.springframework.web.cors.CorsConfiguration();
corsConfiguration.setAllowedOriginPatterns(java.util.List.of("*"));
corsConfiguration.setAllowedMethods(java.util.List.of("GET", "POST", "PUT", "DELETE", "OPTIONS"));
corsConfiguration.setAllowedHeaders(java.util.List.of("*"));
corsConfiguration.setAllowCredentials(true);
return corsConfiguration;
})
).authorizeHttpRequests(authz -> authz
.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
.requestMatchers("/h2-console/**").permitAll()
Expand All @@ -63,7 +67,6 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
//).sessionManagement(s -> s.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
//).exceptionHandling(ex -> ex.authenticationEntryPoint(new AuthEntryPointJwt())
);

http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
return http.build();
}
Expand Down

0 comments on commit 1f0c4b8

Please sign in to comment.