-
Notifications
You must be signed in to change notification settings - Fork 26
Cors configuration for helpline 104 api service #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ab40eeb
5338601
b741ab5
88ffd0e
552198b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,25 @@ | ||||||||||||||||||||||
| package com.iemr.helpline104.config; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| 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.WebMvcConfigurer; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| @Configuration | ||||||||||||||||||||||
| public class CorsConfig implements WebMvcConfigurer { | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| @Value("${cors.allowed-origins}") | ||||||||||||||||||||||
| private String allowedOrigins; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| @Override | ||||||||||||||||||||||
| public void addCorsMappings(CorsRegistry registry) { | ||||||||||||||||||||||
| registry.addMapping("/**") | ||||||||||||||||||||||
| .allowedOriginPatterns(allowedOrigins.split(",")) | ||||||||||||||||||||||
|
Comment on lines
+16
to
+17
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. π οΈ Refactor suggestion Consider validation for empty origins configuration. The code splits - registry.addMapping("/**")
- .allowedOriginPatterns(allowedOrigins.split(","))
+ registry.addMapping("/**")
+ .allowedOriginPatterns(allowedOrigins != null && !allowedOrigins.trim().isEmpty()
+ ? allowedOrigins.split(",")
+ : new String[0])π Committable suggestion
Suggested change
π€ Prompt for AI Agents |
||||||||||||||||||||||
| .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") | ||||||||||||||||||||||
| .allowedHeaders("*") | ||||||||||||||||||||||
| .exposedHeaders("Authorization", "Jwttoken") // Explicitly expose headers if needed | ||||||||||||||||||||||
| .allowCredentials(true) | ||||||||||||||||||||||
| .maxAge(3600) | ||||||||||||||||||||||
|
Comment on lines
+18
to
+22
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. π οΈ Refactor suggestion Review security implications of CORS configuration. The current configuration has several security considerations:
Consider more restrictive settings: .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
- .allowedHeaders("*")
+ .allowedHeaders("Authorization", "Content-Type", "Accept", "Jwttoken")
.exposedHeaders("Authorization", "Jwttoken") // Explicitly expose headers if needed
.allowCredentials(true)
.maxAge(3600)π Committable suggestion
Suggested change
π€ Prompt for AI Agents |
||||||||||||||||||||||
| ; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π‘ Verification agent
π§© Analysis chain
Verify CORS origin pattern validity and consider security implications.
The addition of externalized CORS configuration is a good approach for centralization. However, please verify that the wildcard pattern
http://localhost:*is supported by your CORS implementation, as many CORS libraries don't support port wildcards.Additionally, consider the security implications of allowing all localhost ports, as this could potentially allow access from unintended local applications running on different ports.
π Web query:
π‘ Result:
Based on the search results, Spring Boot's CORS configuration does not natively support wildcard ports in origin patterns like "http://localhost:*". However, there are several approaches to handle this scenario.
CORS Configuration Options in Spring Boot
Spring provides multiple ways to configure CORS with varying levels of flexibility:
Method-Level and Class-Level Configuration
Using the
@CrossOriginannotation, you can specify allowed origins for specific endpoints or controllers:This approach requires you to explicitly specify each allowed origin port (e.g., "http://localhost:4200")[5][7].
Global Configuration
For application-wide CORS settings, you can implement
WebMvcConfigurer:This configuration also requires explicit origin specification[4][5].
Alternative Solutions for Localhost Development
Since Spring Boot doesn't directly support wildcard ports in origins, here are alternatives for local development:
1. Using allowedOriginPatterns
Since Spring Boot 2.4.0, there's a more flexible
allowedOriginPatternsoption that provides more pattern-matching capabilities than the standardallowedOrigins[3]. While it doesn't explicitly support port wildcards, it offers more flexibility.2. Multiple Origin Configuration
You can specify multiple localhost origins with different ports:
3. Feature Request
There is recognition of this need in the community. For instance, there's a feature request in the Kubernetes ingress-nginx project (issue #8264) to allow wildcard ports specifically for localhost origins[6]. This suggests that wildcard port support for localhost would be valuable, but it's not currently a standard feature in Spring Boot.
Best Practices for CORS in Development vs. Production
For development environments:
For production environments:
allowCredentials = true, you cannot use wildcards for origins[7]If you need to test with varying localhost ports, the most straightforward approach is to list all the specific ports you might use during development rather than relying on a wildcard pattern that isn't supported.
Citations:
Update CORS configuration to use supported origin patterns
The pattern
http://localhost:*isnβt recognized by Spring BootβsallowedOrigins. Please adjust as follows:β’ In Spring Boot 2.4+ you can use
allowedOriginPatternswith Ant-style patterns:(This lets you match any localhost port.)
β’ Or explicitly list the ports you need:
cors.allowed-origins=http://localhost:3000,http://localhost:4200,http://localhost:8080Security note: avoid wildcards in productionβonly whitelist specific origins when
allowCredentials=trueand lock down CORS in your prod environment.π€ Prompt for AI Agents