#CORS error
1 messages · Page 1 of 1 (latest)
<@&1004656351647117403> please have a look, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
If you are encountering CORS (Cross-Origin Resource Sharing) errors when deploying your Java Spring project with a built frontend, it means that the frontend is making requests to the backend from a different origin (domain, protocol, or port) than the one it was served from.
To resolve this issue, you need to configure CORS settings in your Java Spring backend to allow requests from the frontend's origin. Here's how you can do it:
- Add the
@CrossOriginannotation to your controller class or specific handler methods. For example:
@RestController
@CrossOrigin(origins = "http://frontend-domain.com")
public class YourController {
// Controller methods...
}
Replace "http://frontend-domain.com" with the actual domain of your frontend.
-
If you want to allow requests from any origin, you can use
@CrossOrigin(origins = "*"). However, this should only be used for development purposes and not in production. -
If you need more fine-grained control over CORS settings, you can create a separate configuration class and extend
WebMvcConfigurerAdapter. Here's an example:
@Configuration
public class CorsConfig extends WebMvcConfigurerAdapter {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://frontend-domain.com")
.allowedMethods("GET", "POST", "PUT", "DELETE")
.allowedHeaders("*");
}
}
Again, replace "http://frontend-domain.com" with your actual frontend domain.
- If your backend is using Spring Security, make sure to configure CORS settings there as well. You can do this by extending
WebSecurityConfigurerAdapterand overriding theconfigure(HttpSecurity http)method. For example:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable();
}
}
This will disable CSRF protection and enable CORS.
After making these changes, rebuild and redeploy your project, and the CORS errors should be resolved.
Hi there
If you arent using spring security
HAHAHAH
Aah it's you!
i am not
let's hop on last post
alright, try with that @CrossOrigin annotation first
also follow what AI said
it may help ya
i have it everywhere
Fetch API
🤣 🤣 🤣 🤣 😂
not recommended for react today
bruh. On react server people said, that axios and Jquery suck
Changed the category to Frameworks.
<@&987246452180930620> please have a look, thanks.
yeah
but there are more powerful tools
React Query is often described as the missing data-fetching library for React, but in more technical terms, it makes fetching, caching, synchronizing and updating server state in your React applications a breeze.
Motivation
such tools, have integrated controls for cors, headrs, caching etc..
by the way, webconfig, that u suggested crashes by code
is there any error ?
i thought i've asked you already
why are you using that path pattern ?
and u didn't answer me
¯_(ツ)_/¯
buecase dude, whose github i sent you, uses it to output frontend
i mean this: spring.mvc.pathmatch.matching
why are you setting it explicitly like this ?
the same reason. i just followed instructions
instructions of WHAT ?,
of packaging react app into spring boot app
that doesn't make any sense
it's irrelevent
do you remember video, that i dm'd u?
no
this all comes from that video
Looking to learn how to package a React.js frontend application within your Spring Boot server? You're in the right place! In this tutorial, we'll show you step-by-step how to develop and bundle a JavaScript frontend single page application (SPA) built with React, all while embedding the finished result within a Spring Boot project.
Whether you...
sorry dnt have time to watch
last 10 mins
maybe, i trust you
do not follow youtube
ok
okey
actually interesting. Just found stackoverflow post with the same pom.xml code
@oak drift
i fixed it
only error with CORS is left
if you have @CrossOrigin("*") in our server app
then the problem now is in frontend
you have to handwrite some controls to allow cors, there are some headers
and mode
that's why i suggest to use a smart library like React Query
the frontend has grown up today, it becomes a whole ecosystem
not like the old days
i will try
everything evolve fast now, os, browser, ide, frameworks
As you can see, i am at the beginning of my coding journey, and sometimes looking at those growing technologies like AI makes me feel kind of useless or replaceable in few years or decades.
And usually i just become insecure about my decision ater those thoughts
never mind