Skip to content

Commit

Permalink
Test Spring-only config.
Browse files Browse the repository at this point in the history
  • Loading branch information
samie committed Jul 26, 2024
1 parent 9bfb790 commit 98011b9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 76 deletions.
47 changes: 47 additions & 0 deletions src/main/java/com/example/application/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,25 @@

import com.vaadin.flow.component.page.AppShellConfigurator;
import com.vaadin.flow.theme.Theme;
import jakarta.servlet.Filter;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.tomcat.util.http.Rfc6265CookieProcessor;
import org.apache.tomcat.util.http.SameSiteCookies;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
import org.springframework.boot.web.servlet.server.CookieSameSiteSupplier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Arrays;

/**
* The entry point of the Spring Boot application.
Expand All @@ -13,10 +30,40 @@
*
*/
@SpringBootApplication
@CrossOrigin(origins = "https://samie.github.io", maxAge = 3600)
public class Application implements AppShellConfigurator {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Bean
public TomcatContextCustomizer sessionCookieConfigForCors() {
return context -> {
final Rfc6265CookieProcessor cookieProcessor = new Rfc6265CookieProcessor() {
@Override
public String generateHeader(Cookie cookie, HttpServletRequest request) {

// Needs to be secure
if (cookie.getName().startsWith("JSESSIONID")) {
cookie.setSecure(true);
cookie.setAttribute("SameSite", SameSiteCookies.NONE.getValue());
}
return super.generateHeader(cookie, request);
}
};
context.setCookieProcessor(cookieProcessor);
};
}

@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("https://samie.github.io"));
configuration.setAllowedMethods(Arrays.asList("GET", "POST"));
configuration.setAllowedHeaders(Arrays.asList("*"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
}
76 changes: 0 additions & 76 deletions src/main/java/com/example/application/CORSFilter.java

This file was deleted.

0 comments on commit 98011b9

Please sign in to comment.