Last active
June 22, 2024 18:22
-
-
Save LuisCusihuaman/13083e5985f226b3ba8427a891f5b6a5 to your computer and use it in GitHub Desktop.
CORS Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/main/java/com/edu/uba/projects/config/AppConfig.java | |
package com.edu.uba.projects.config; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.security.config.Customizer; | |
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.configurers.AbstractHttpConfigurer; | |
import org.springframework.security.web.SecurityFilterChain; | |
import org.springframework.web.cors.CorsConfiguration; | |
import org.springframework.web.cors.UrlBasedCorsConfigurationSource; | |
import org.springframework.web.filter.CorsFilter; | |
import java.util.Collections; | |
@Configuration | |
@EnableWebSecurity | |
public class SecurityConfig { | |
@Bean | |
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { | |
http | |
.authorizeHttpRequests(authorizeRequests -> | |
authorizeRequests | |
.requestMatchers("/swagger-ui.html", "/swagger-ui/**", "/swagger-resources/**", "/swagger-resources", "/v3/api-docs/**", "/proxy/**").permitAll() // Allow access without authentication for Swagger | |
.anyRequest().permitAll() // Allow all other requests without authentication | |
) | |
.cors(Customizer.withDefaults()) | |
.csrf(AbstractHttpConfigurer::disable); | |
return http.build(); | |
} | |
@Bean | |
public CorsFilter corsFilter() { | |
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); | |
CorsConfiguration config = new CorsConfiguration(); | |
config.setAllowCredentials(true); | |
config.setAllowedOriginPatterns(Collections.singletonList("*")); // Allow all origins | |
config.addAllowedHeader("*"); | |
config.addAllowedMethod("*"); | |
source.registerCorsConfiguration("/**", config); | |
return new CorsFilter(source); | |
} | |
} | |
/// src/main/java/com/edu/uba/projects/ProjectsApplication.java | |
package com.edu.uba.projects; | |
import io.swagger.v3.oas.annotations.OpenAPIDefinition; | |
import io.swagger.v3.oas.annotations.servers.Server; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.web.bind.annotation.CrossOrigin; | |
@OpenAPIDefinition(servers = {@Server(url = "/", description = "Default Server URL")}) | |
@CrossOrigin(origins = "*", allowedHeaders = "*") | |
@SpringBootApplication | |
public class ProjectsApplication { | |
public static void main(String[] args) { | |
SpringApplication.run(ProjectsApplication.class, args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
gradle dependencies: