Skip to content

Commit

Permalink
[fix] CORS 관련 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
nimikgnoej committed Aug 31, 2023
1 parent e6dc261 commit 899daf2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.example.taskqueue.oauth.handler.OAuth2LoginFailureHandler;
import com.example.taskqueue.oauth.handler.OAuth2LoginSuccessHandler;
import com.example.taskqueue.security.ResponseUtils;
import com.example.taskqueue.security.filter.CorsFilter;
import com.example.taskqueue.security.filter.JwtAuthenticationProcessingFilter;
import com.example.taskqueue.oauth.jwt.JwtService;
import com.example.taskqueue.oauth.login.filter.CustomJsonUsernamePasswordAuthenticationFilter;
Expand Down Expand Up @@ -172,4 +173,9 @@ public JwtAuthenticationProcessingFilter jwtAuthenticationProcessingFilter() {
JwtAuthenticationProcessingFilter jwtAuthenticationFilter = new JwtAuthenticationProcessingFilter(jwtService, userRepository,responseUtils);
return jwtAuthenticationFilter;
}

@Bean
public CorsFilter corsFilter() {
return new CorsFilter();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.taskqueue.security.filter;

import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.util.logging.LogRecord;

@Component
@WebFilter("/*")
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CorsFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletResponse httpResponse = (HttpServletResponse) response; // Cast to HttpServletResponse
// Add the necessary CORS headers
httpResponse.setHeader("Access-Control-Allow-Origin", "http://localhost:3000"); // Adjust this based on your needs
httpResponse.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
httpResponse.setHeader("Access-Control-Allow-Headers", "Authorization, Content-Type");
httpResponse.setHeader("Access-Control-Allow-Credentials", "true");

// Continue with the request
chain.doFilter(request, response);
}
}

0 comments on commit 899daf2

Please sign in to comment.