-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6dc261
commit 899daf2
Showing
2 changed files
with
40 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
34 changes: 34 additions & 0 deletions
34
src/main/java/com/example/taskqueue/security/filter/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,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); | ||
} | ||
} |