Skip to content

Commit f11d2aa

Browse files
author
Pavan Jadda
committed
Add CORS config
1 parent 746c6ee commit f11d2aa

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/main/java/com/pj/keycloak/security/SecurityConfig.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
import org.springframework.security.core.session.SessionRegistryImpl;
1717
import org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy;
1818
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy;
19+
import org.springframework.web.cors.CorsConfiguration;
20+
import org.springframework.web.cors.CorsConfigurationSource;
21+
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
22+
23+
import java.util.Collections;
1924

2025
@Configuration
2126
@EnableWebSecurity
@@ -54,5 +59,30 @@ protected void configure(HttpSecurity http) throws Exception
5459
.hasAnyRole(Roles.ADMIN)
5560
.anyRequest()
5661
.permitAll();
62+
63+
http.cors();
5764
}
65+
//Cors filter to accept incoming requests
66+
@Bean
67+
CorsConfigurationSource corsConfigurationSource()
68+
{
69+
CorsConfiguration configuration = new CorsConfiguration();
70+
configuration.applyPermitDefaultValues();
71+
configuration.setAllowedMethods(Collections.singletonList("*"));
72+
configuration.setAllowCredentials(true);
73+
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
74+
source.registerCorsConfiguration("/**", configuration);
75+
return source;
76+
}
77+
78+
/* public WebMvcConfigurer corsConfigurer()
79+
{
80+
return new WebMvcConfigurer() {
81+
@Override
82+
public void addCorsMappings(CorsRegistry registry)
83+
{
84+
registry.addMapping("/api/*").allowedOrigins("http://localhost:4200");
85+
}
86+
};
87+
}*/
5888
}

0 commit comments

Comments
 (0)