Advertisement
mauroslucios

SecurityConfig

Dec 21st, 2021
671
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.74 KB | None | 0 0
  1. package com.devsuperiror.dsdeliver.config;
  2.  
  3. import java.util.Arrays;
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.core.env.Environment;
  9. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  10. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  11. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  12. import org.springframework.security.config.http.SessionCreationPolicy;
  13. import org.springframework.web.cors.CorsConfiguration;
  14. import org.springframework.web.cors.CorsConfigurationSource;
  15. import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
  16.  
  17. @Configuration
  18. @EnableWebSecurity
  19. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  20.  
  21.     @Autowired
  22.     private Environment env;
  23.  
  24.     @Override
  25.     protected void configure(HttpSecurity http) throws Exception {
  26.         if (Arrays.asList(env.getActiveProfiles()).contains("test")) {
  27.             http.headers().frameOptions().disable();
  28.         }
  29.        
  30.         http.cors().and().csrf().disable();
  31.         http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
  32.         http.authorizeRequests().anyRequest().permitAll();
  33.     }
  34.  
  35.     @Bean
  36.     CorsConfigurationSource corsConfigurationSource() {
  37.         CorsConfiguration configuration = new CorsConfiguration().applyPermitDefaultValues();
  38.         configuration.setAllowedMethods(Arrays.asList("POST", "GET", "PUT", "DELETE", "OPTIONS"));
  39.         final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  40.         source.registerCorsConfiguration("/**", configuration);
  41.         return source;
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement