updated code for cors
This commit is contained in:
@@ -2,18 +2,9 @@ package net.gepafin.tendermanagement;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
|
||||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
||||||
|
|
||||||
@EnableScheduling
|
@EnableScheduling
|
||||||
@ComponentScan(basePackages = {"net.gepafin.tendermanagement"})
|
|
||||||
@EnableJpaRepositories(basePackages = {"net.gepafin.tendermanagement"})
|
|
||||||
@EntityScan(basePackages = {"net.gepafin.tendermanagement"})
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class TendermanagementApplication {
|
public class TendermanagementApplication {
|
||||||
|
|
||||||
@@ -21,17 +12,6 @@ public class TendermanagementApplication {
|
|||||||
SpringApplication.run(TendermanagementApplication.class, args);
|
SpringApplication.run(TendermanagementApplication.class, args);
|
||||||
System.out.println("Spring Boot started");
|
System.out.println("Spring Boot started");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
|
||||||
public class CorsConfig implements WebMvcConfigurer {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addCorsMappings(CorsRegistry registry) {
|
|
||||||
|
|
||||||
registry.addMapping("/**").allowedOrigins("http://localhost:3000")
|
|
||||||
.allowedMethods("GET", "POST", "PUT", "DELETE", "HEAD").allowCredentials(true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,9 +15,14 @@ import org.springframework.security.config.http.SessionCreationPolicy;
|
|||||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.security.web.SecurityFilterChain;
|
import org.springframework.security.web.SecurityFilterChain;
|
||||||
|
import org.springframework.security.web.access.intercept.AuthorizationFilter;
|
||||||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
||||||
import org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher;
|
import org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher;
|
||||||
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
||||||
|
import org.springframework.web.cors.CorsConfiguration;
|
||||||
|
import org.springframework.web.cors.CorsConfigurationSource;
|
||||||
|
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
||||||
|
import org.springframework.web.filter.CorsFilter;
|
||||||
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;
|
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;
|
||||||
|
|
||||||
import io.swagger.v3.oas.models.Components;
|
import io.swagger.v3.oas.models.Components;
|
||||||
@@ -65,18 +70,17 @@ public class SecurityConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//
|
@Bean
|
||||||
// @Bean
|
public CorsConfigurationSource corsConfigurationSource() {
|
||||||
// public CorsFilter corsFilter() {
|
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||||
// UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
CorsConfiguration config = new CorsConfiguration();
|
||||||
// CorsConfiguration config = new CorsConfiguration();
|
config.setAllowCredentials(true);
|
||||||
// config.setAllowCredentials(true);
|
config.addAllowedOrigin("http://localhost:3000"); // Change this to your frontend URL
|
||||||
// config.addAllowedOrigin("*");
|
config.addAllowedHeader("*");
|
||||||
// config.addAllowedHeader("*");
|
config.addAllowedMethod("*");
|
||||||
// config.addAllowedMethod("*");
|
source.registerCorsConfiguration("/**", config);
|
||||||
// source.registerCorsConfiguration("/**", config);
|
return source;
|
||||||
// return new CorsFilter(source);
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SecurityFilterChain securityFilterChain(HttpSecurity http, MvcRequestMatcher.Builder mvc) throws Exception {
|
public SecurityFilterChain securityFilterChain(HttpSecurity http, MvcRequestMatcher.Builder mvc) throws Exception {
|
||||||
@@ -92,6 +96,7 @@ public class SecurityConfig {
|
|||||||
.sessionManagement(session -> session
|
.sessionManagement(session -> session
|
||||||
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
|
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
|
||||||
)
|
)
|
||||||
|
.addFilterBefore(new CorsFilter(corsConfigurationSource()), AuthorizationFilter.class)
|
||||||
.addFilterBefore(new JWTFilter(tokenProvider), UsernamePasswordAuthenticationFilter.class);
|
.addFilterBefore(new JWTFilter(tokenProvider), UsernamePasswordAuthenticationFilter.class);
|
||||||
|
|
||||||
return http.build();
|
return http.build();
|
||||||
|
|||||||
Reference in New Issue
Block a user