Skip to content

Commit

Permalink
Merge pull request #6969 from ita-social-projects/security-fix
Browse files Browse the repository at this point in the history
SecurityConfig update
  • Loading branch information
HelenSotnik authored Feb 17, 2024
2 parents b5eddf8 + aa24694 commit f66a551
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions core/src/main/java/greencity/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.springframework.security.config.annotation.authentication.configuration.EnableGlobalAuthentication;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
Expand Down Expand Up @@ -87,13 +86,14 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
CorsConfiguration config = new CorsConfiguration();
config.setAllowedOrigins(Collections.singletonList("http://localhost:4200"));
config.setAllowedOrigins(Collections.singletonList("http://localhost:4205"));
config.setAllowedOrigins(Collections.singletonList("*"));
config.setAllowedMethods(
Arrays.asList("GET", "POST", "OPTIONS", "DELETE", "PUT", "PATCH"));
config.setAllowedHeaders(
Arrays.asList("Access-Control-Allow-Origin", "Access-Control-Allow-Headers",
"X-Requested-With", "Origin", "Content-Type", "Accept", "Authorization"));
config.setAllowCredentials(true);
config.setAllowedHeaders(Collections.singletonList("*"));
config.setAllowCredentials(true);
config.setMaxAge(3600L);
return config;
}))
Expand All @@ -105,9 +105,18 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
.sendError(SC_UNAUTHORIZED, "Authorize first."))
.accessDeniedHandler((req, resp, exc) -> resp.sendError(SC_FORBIDDEN, "You don't have authorities.")))
.authorizeHttpRequests(req -> req
.requestMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.requestMatchers("/", "/management/", "/management/login").permitAll()
.requestMatchers("/management/**")
.hasAnyRole(ADMIN)
.requestMatchers("/v2/api-docs/**",
"/v3/api-docs/**",
"/swagger.json",
"/swagger-ui.html",
"/swagger-ui/**",
"/swagger-resources/**",
"/webjars/**")
.permitAll()
.requestMatchers("/css/**",
"/img/**")
.permitAll()
Expand Down Expand Up @@ -385,24 +394,6 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
return http.build();
}

/**
* Method for configure matchers that will be ignored in security.
*
* @return {@link WebSecurityCustomizer}
*/
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return web -> {
web.ignoring().requestMatchers("/v2/api-docs/**");
web.ignoring().requestMatchers("/v3/api-docs/**");
web.ignoring().requestMatchers("/swagger.json");
web.ignoring().requestMatchers("/swagger-ui.html");
web.ignoring().requestMatchers("/swagger-resources/**");
web.ignoring().requestMatchers("/webjars/**");
web.ignoring().requestMatchers("/swagger-ui/**");
};
}

/**
* Method for configure type of authentication provider.
*
Expand Down

0 comments on commit f66a551

Please sign in to comment.