Skip to content

Commit

Permalink
cors filter for quarkus and spring boot archetype (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tihomir Surdilovic authored and mswiderski committed Apr 11, 2019
1 parent d62974a commit 8339c98
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
Expand Up @@ -30,6 +30,9 @@ under the License.
<include>README.md</include>
</includes>
</fileSet>
<fileSet filtered="true" packaged="true">
<directory>src/main/java</directory>
</fileSet>
<fileSet filtered="true" packaged="false" encoding="UTF-8">
<directory>src/main/resources</directory>
<includes>
Expand Down
@@ -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() {

}
}
@@ -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);
}
}

0 comments on commit 8339c98

Please sign in to comment.