Skip to content

Commit

Permalink
perf($Spring): refine CORS configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Jul 12, 2021
1 parent ad04895 commit cbb9c7c
Showing 1 changed file with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.jmsoftware.maf.springcloudstarter.configuration;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import static org.springframework.web.cors.CorsConfiguration.ALL;

/**
* <h1>WebMvcConfiguration</h1>
* <p>
Expand All @@ -12,10 +15,12 @@
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com
* @date 1/23/20 9:02 AM
**/
@Slf4j
@RequiredArgsConstructor
public class WebMvcConfiguration implements WebMvcConfigurer {
/**
* Max age: 3600 seconds (1 hour)
* Max age: 3600 seconds (1 hour). Configure how long in seconds the response from a pre-flight request
* can be cached by clients. By default this is set to 1800 seconds (30 minutes).
*/
private static final long MAX_AGE_SECS = 3600;

Expand All @@ -25,11 +30,12 @@ public class WebMvcConfiguration implements WebMvcConfigurer {
* @param registry CORS registry
*/
@Override
@SuppressWarnings("BroadCORSAllowOrigin")
public void addCorsMappings(CorsRegistry registry) {
log.info("Configuring CORS allowedOrigins: {}, allowedMethods: {}, allowedHeaders: {}", ALL, ALL, ALL);
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("HEAD", "OPTIONS", "GET", "POST", "PUT", "PATCH", "DELETE")
.allowedOrigins(ALL)
.allowedMethods(ALL)
.allowedHeaders(ALL)
.maxAge(MAX_AGE_SECS);
}
}

0 comments on commit cbb9c7c

Please sign in to comment.