-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
71 additions
and
1 deletion.
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
27 changes: 27 additions & 0 deletions
27
libs/jarc-auth-lib/src/main/java/com/sourcefuse/jarc/authlib/cors/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,27 @@ | ||
package com.sourcefuse.jarc.authlib.cors; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.CorsRegistry; | ||
import org.springframework.web.servlet.config.annotation.EnableWebMvc; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
@EnableWebMvc | ||
public class CorsConfig implements WebMvcConfigurer { | ||
|
||
@Value("${cors.url:#{null}}") | ||
String corsUrl; | ||
|
||
@Override | ||
public void addCorsMappings(CorsRegistry registry) { | ||
if (corsUrl != null) { | ||
registry | ||
.addMapping("/**") | ||
.allowedOrigins(corsUrl) | ||
.allowedMethods("*") | ||
.allowedHeaders("*") | ||
.allowCredentials(true); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
libs/jarc-auth-lib/src/main/java/com/sourcefuse/jarc/authlib/cors/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,33 @@ | ||
package com.sourcefuse.jarc.authlib.cors; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
@Service | ||
public class CorsFilter extends OncePerRequestFilter { | ||
|
||
@Value("${cors.url:#{null}}") | ||
String corsUrl; | ||
|
||
@Override | ||
protected void doFilterInternal( | ||
HttpServletRequest request, | ||
HttpServletResponse response, | ||
FilterChain filterChain | ||
) throws ServletException, IOException { | ||
if (corsUrl != null) { | ||
response.setHeader("Access-Control-Allow-Origin", corsUrl); | ||
response.setHeader("Access-Control-Allow-Methods", "*"); | ||
response.setHeader("Access-Control-Allow-Headers", "*"); | ||
response.setHeader("Access-Control-Allow-Credentials", "true"); | ||
} | ||
|
||
filterChain.doFilter(request, response); | ||
} | ||
} |
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