Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cors filter for quarkus and spring boot archetype (#16)
- Loading branch information
1 parent
d62974a
commit 8339c98
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
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
69 changes: 69 additions & 0 deletions
69
...as-quarkus-archetype/src/main/resources/archetype-resources/src/main/java/CorsFilter.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package ${package}; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.servlet.Filter; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.FilterConfig; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.annotation.WebFilter; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
@WebFilter(filterName = "CorsFilter", urlPatterns = "/*", asyncSupported = true) | ||
public class CorsFilter implements Filter { | ||
|
||
private static final String ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin"; | ||
|
||
private static final String ACCESS_CONTROL_ALLOW_CREDENTIALS = "Access-Control-Allow-Credentials"; | ||
|
||
private static final String ACCESS_CONTROL_ALLOW_METHODS = "Access-Control-Allow-Methods"; | ||
|
||
private static final String ACCESS_CONTROL_ALLOW_HEADERS = "Access-Control-Allow-Headers"; | ||
|
||
private static final String ORIGIN = "Origin"; | ||
|
||
private static final String ACCESS_CONTROL_REQUEST_METHOD = "Access-Control-Request-Method"; | ||
|
||
private static final String ACCESS_CONTROL_EXPOSE_HEADERS = "Access-Control-Expose-Headers"; | ||
|
||
private static final String ACCESS_CONTROL_REQUEST_HEADERS = "Access-Control-Request-Headers"; | ||
|
||
@Override | ||
public void init(FilterConfig filterConfig) { | ||
} | ||
|
||
@Override | ||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { | ||
HttpServletRequest request = (HttpServletRequest) servletRequest; | ||
HttpServletResponse response = (HttpServletResponse) servletResponse; | ||
String origin = request.getHeader(ORIGIN); | ||
if (origin == null) { | ||
chain.doFilter(servletRequest, servletResponse); | ||
} else { | ||
String requestMethods = request.getHeader(ACCESS_CONTROL_REQUEST_METHOD); | ||
if (requestMethods != null) { | ||
response.setHeader(ACCESS_CONTROL_ALLOW_METHODS, requestMethods); | ||
} | ||
String allowHeaders = request.getHeader(ACCESS_CONTROL_REQUEST_HEADERS); | ||
if (allowHeaders != null) { | ||
response.setHeader(ACCESS_CONTROL_ALLOW_HEADERS, allowHeaders); | ||
} | ||
response.setHeader(ACCESS_CONTROL_ALLOW_ORIGIN, origin); | ||
response.setHeader(ACCESS_CONTROL_ALLOW_CREDENTIALS, "true"); | ||
response.setHeader(ACCESS_CONTROL_EXPOSE_HEADERS, "Content-Disposition"); | ||
if (!"OPTIONS".equalsIgnoreCase(request.getMethod())) { | ||
chain.doFilter(servletRequest, servletResponse); | ||
} else { | ||
response.flushBuffer(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
|
||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...springboot-archetype/src/main/resources/archetype-resources/src/main/java/CorsConfig.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package ${package}; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.cors.CorsConfiguration; | ||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource; | ||
import org.springframework.web.filter.CorsFilter; | ||
|
||
@Configuration | ||
public class CorsConfig { | ||
|
||
@Bean | ||
public CorsFilter corsFilter() { | ||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); | ||
|
||
CorsConfiguration config = new CorsConfiguration(); | ||
config.setAllowCredentials(true); | ||
config.addAllowedOrigin("*"); | ||
config.addAllowedHeader("*"); | ||
config.addAllowedMethod("OPTIONS"); | ||
config.addAllowedMethod("GET"); | ||
config.addAllowedMethod("POST"); | ||
config.addAllowedMethod("PUT"); | ||
config.addAllowedMethod("DELETE"); | ||
source.registerCorsConfiguration("/**", config); | ||
|
||
return new CorsFilter(source); | ||
} | ||
} |