Image error solve update

This commit is contained in:
2025-10-10 11:33:53 +05:30
parent 94c4f03455
commit 279f025432
2 changed files with 6 additions and 22 deletions

View File

@ -23,9 +23,8 @@ import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import static org.springframework.http.HttpMethod.*; import static org.springframework.http.HttpMethod.POST;
@EnableWebSecurity @EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true) @EnableGlobalMethodSecurity(prePostEnabled = true)
@ -48,14 +47,13 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override @Override
protected void configure(HttpSecurity http) throws Exception { protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable(); http.csrf().disable();
// ✅ Enable Spring Security CORS support
http.cors(); http.cors();
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.authorizeRequests() http.authorizeRequests()
.antMatchers(publicUrls).permitAll() .antMatchers(publicUrls).permitAll()
.antMatchers(POST, "/api/files/upload").permitAll() // Allow file uploads
.anyRequest().authenticated(); .anyRequest().authenticated();
http.exceptionHandling() http.exceptionHandling()
@ -76,7 +74,6 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
return super.authenticationManagerBean(); return super.authenticationManagerBean();
} }
// ✅ This is the correct, Security-compatible CORS configuration
@Bean @Bean
public CorsConfigurationSource corsConfigurationSource() { public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration config = new CorsConfiguration(); CorsConfiguration config = new CorsConfiguration();
@ -85,11 +82,10 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
config.setAllowedHeaders(Arrays.asList("*")); config.setAllowedHeaders(Arrays.asList("*"));
config.setExposedHeaders(Arrays.asList(SecurityConstants.JWT_TOKEN_HEADER)); config.setExposedHeaders(Arrays.asList(SecurityConstants.JWT_TOKEN_HEADER));
config.setAllowCredentials(true); config.setAllowCredentials(true);
config.setMaxAge(3600L); // Cache preflight for 1 hour config.setMaxAge(3600L); // 1 hour
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config); source.registerCorsConfiguration("/**", config);
return source; return source;
} }
} }

View File

@ -2,7 +2,6 @@ package net.shyshkin.study.fullstack.supportportal.backend.config;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@ -14,28 +13,17 @@ public class WebConfig implements WebMvcConfigurer {
@Value("${file.upload.directory}") @Value("${file.upload.directory}")
private String uploadDirectory; private String uploadDirectory;
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins(
"https://cmcadminfrontend.rootxwire.com",
"https://maincmc.rootxwire.com",
"http://localhost:4200"
)
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("*")
.allowCredentials(true);
}
@Override @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) { public void addResourceHandlers(ResourceHandlerRegistry registry) {
// Ensure the path ends with a separator
String uploadPath = uploadDirectory; String uploadPath = uploadDirectory;
if (!uploadPath.endsWith(File.separator)) { if (!uploadPath.endsWith(File.separator)) {
uploadPath += File.separator; uploadPath += File.separator;
} }
// Serve uploaded files under /uploads/**
registry.addResourceHandler("/uploads/**") registry.addResourceHandler("/uploads/**")
.addResourceLocations("file:" + uploadPath) .addResourceLocations("file:" + uploadPath)
.setCachePeriod(3600); .setCachePeriod(3600); // Cache for 1 hour
} }
} }