From 9be69459ce0aa6e8cce52ce89276a031a99d7dc8 Mon Sep 17 00:00:00 2001 From: byeon22 Date: Fri, 26 Jan 2024 15:39:28 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20CORS=20=EC=97=90=EB=9F=AC=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../oot/custom/config/CorsFilter.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/main/java/com/endlesshorses/oot/custom/config/CorsFilter.java diff --git a/src/main/java/com/endlesshorses/oot/custom/config/CorsFilter.java b/src/main/java/com/endlesshorses/oot/custom/config/CorsFilter.java new file mode 100644 index 0000000..348f3b6 --- /dev/null +++ b/src/main/java/com/endlesshorses/oot/custom/config/CorsFilter.java @@ -0,0 +1,52 @@ +package com.endlesshorses.oot.custom.config; + +import java.io.IOException; + +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; + +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.FilterConfig; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +@Component +@Order(Ordered.HIGHEST_PRECEDENCE) +public class CorsFilter implements Filter { + private Object Ordered; + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + Filter.super.init(filterConfig); + } + + @Override + public void destroy() { + Filter.super.destroy(); + } + + @Override + public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { + HttpServletRequest request = (HttpServletRequest) req; + HttpServletResponse response = (HttpServletResponse) res; + + response.setHeader("Access-Control-Allow-Origin", "*"); + response.setHeader("Access-Control-Allow-Credentials", "true"); + response.setHeader("Access-Control-Allow-Methods", "*"); + response.setHeader("Access-Control-Max-Age", "3600"); + response.setHeader("Access-Control-Allow-Headers", + "Origin, X-Requested-With, Content-Type, Accept, Authorization"); + + if ("OPTIONS".equalsIgnoreCase(request.getMethod())) { + response.setStatus(HttpServletResponse.SC_OK); + } else { + chain.doFilter(req, res); + } + + } +} \ No newline at end of file