Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove cors/csrf java code configuration, in favor of regular spring-cloud-gateway configuration #59

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@
import lombok.extern.slf4j.Slf4j;
import org.georchestra.gateway.model.GatewayConfigProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.server.SecurityWebFilterChain;
import org.springframework.security.web.server.authentication.logout.ServerLogoutSuccessHandler;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -61,16 +66,31 @@ public class GatewaySecurityConfiguration {
@Autowired(required = false)
ServerLogoutSuccessHandler oidcLogoutSuccessHandler;

private @Value("${georchestra.gateway.csrfEnabled:false}") boolean csrfEnabled;
private @Value("${georchestra.gateway.corsEnabled:false}") boolean corsEnabled;

@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http,
List<ServerHttpSecurityCustomizer> customizers) throws Exception {

log.info("Initializing security filter chain...");
// disable csrf and cors or the websocket connection gets a 403 Forbidden.
// Revisit.
log.info("CSRF and CORS disabled. Revisit how they interfer with Websockets proxying.");
http.csrf().disable().cors().disable();

if (!csrfEnabled) {
log.info("CSRF disabled. Revisit how they interfer with Websockets proxying.");
http.csrf().disable();
}
if (!corsEnabled) {
log.info("CORS disabled. Revisit how they interfer with Websockets proxying.");
http.cors().disable();
} else {
CorsConfiguration config = new CorsConfiguration();
config.addAllowedOrigin(CorsConfiguration.ALL);
config.addAllowedHeader("*");
config.addAllowedMethod("*");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
http.cors().configurationSource(source);
}
pmauduit marked this conversation as resolved.
Show resolved Hide resolved
http.formLogin()
.authenticationFailureHandler(new ExtendedRedirectServerAuthenticationFailureHandler("login?error"))
.loginPage("/login");
Expand Down
6 changes: 4 additions & 2 deletions gateway/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ server:
port: 8080
compression.enabled: true
# HTTP/2 is only supported over TLS (HTTPS)
# So we need to configure SSL if we want to support HTTP/2
# So we need to configure SSL if we want to support HTTP/2
http2.enabled: ${server.ssl.enabled}
ssl:
enabled: false
#TODO: configure SSL with a self-signed certificate

spring:
config:
import: optional:file:${georchestra.datadir}/default.properties,optional:file:${georchestra.datadir}/gateway/gateway.yaml,optional:file:${georchestra.datadir}/gateway/security.yaml
Expand Down Expand Up @@ -54,6 +54,8 @@ spring:

georchestra:
gateway:
csrfEnabled: false
corsEnabled: false
security:
oauth2:
enabled: false
Expand Down