score:3
you can add it by overriding addcorsmappings
of webmvcconfigureradapter
, so either create a class that extends webmvcconfigureradapter
or define a bean in your configuration class like this:
@bean
public webmvcconfigurer corsconfigurer () {
return new webmvcconfigureradapter() {
@override
public void addcorsmappings(corsregistry registry) {
registry.addmapping("/api/**")
.allowedorigins("http://domain1.com", "http://domain2.com")
.allowedmethods("get", "options")
.allowedheaders("header1", "header2", "header3")
.exposedheaders("header1", "header2")
.allowcredentials(false).maxage(3600);
}
}
}
edit
as of 5.0 webmvcconfigureradapter
is deprecated and hence you could acheive the same thing by implementing webmvcconfigurer
interface (added default methods, thanks java 8 ! and can be implemented directly without the need for this adapter)
@configuration
@enablewebmvc
public class mywebmvcconfig implements webmvcconfigurer {
@override
public void addcorsmappings(corsregistry registry) {
registry.addmapping("/api/**")
.allowedorigins("http://domain1.com", "http://domain2.com")
.allowedmethods("get", "options")
.allowedheaders("header1", "header2", "header3")
.exposedheaders("header1", "header2")
.allowcredentials(false).maxage(3600);
}
}
score:0
you can add a global configuration like
@bean
public webmvcconfigurer corsconfigurer() {
return new webmvcconfigureradapter() {
@override
public void addcorsmappings(corsregistry registry) {
registry.addmapping("/greeting-javaconfig").allowedorigins("http://localhost:9000");
}
};
}
just use this to add a global corrs configuration that will affect all the endpoints.try this if the annotation doesn't work.
score:2
you can create separate cors configuration class as follows. this class will handle the cors configurations for all requests throughout your application and you need not annotate each controller separately with @crossorigin
.
@configuration
public class corsconfig {
@bean
public corsfilter corsfilter() {
urlbasedcorsconfigurationsource source = new urlbasedcorsconfigurationsource();
corsconfiguration config = new corsconfiguration();
config.setallowcredentials(true);
config.addallowedorigin("*"); //'*' allows all endpoints, provide your url/endpoint, if any.
config.addallowedheader("*");
config.addallowedmethod("post"); //add the methods you want to allow like 'get', 'put',etc. using similar statements.
config.addallowedmethod("get");
source.registercorsconfiguration("/**", config);
return new corsfilter(source);
}
}
Source: stackoverflow.com
Related Query
- Annotation CrossOrigin not working in Spring boot
- http-proxy-middleware proxy is not working in React js and Spring Boot project. GET API is return 415 status error
- Spring Boot 2.1.0 only serve index.html if resource not resolved (SPA, react-router)
- React App + Spring Boot - JWT auth token inside a cookie is not set in Chrome
- Route not defined react router and spring boot server
- Spring boot does not receive headers from react js
- Spring Boot REST - Required String parameter is not present
- React front end is not communicating with spring boot REST API. Axios network err in console
- Required request part 'image' is not present with React and Spring Boot
- How to use the react spring hook with react. Basic code not working
- Spring animation is not working in ReactJS
- npm run build not adding Spring Boot context path
- Not able to make spring boot take limited parameters while calling API from UI to backend using @RequestParam
- React / React Spring List Animate Out Not Working
- Spring Boot Server not loading React webpage
- spring boot and reactjs for frontend was working fine till i add security to the backend
- React Spring Transition not working properly
- Annotation plugin not working with Chart.js 2.8.0
- React Spring translate animation not working and clicking on a list item seems to be delayed
- React Spring useTransition not working as expected
- Spring boot with react url not found issue
- Not getting data by axios.get() request in reactjs from spring boot backend
- Current request is not a multipart request : Spring boot
- There was an unexpected error (type=Method Not Allowed, status=405). Request method 'GET' not supported with spring boot
- CORS error when post request is send, but not get request in Spring Boot and Reactjs
- Spring boot not forwarding url path to index.html React bundle
-   jsx not working
- onKeyDown event not working on divs in React
- create-react-app is not working since version 4.0.1
- Formik onSubmit function is not working on my code
More Query from same tag
- Manipulating dom elements dynamically in react?
- Javascript function- transform from one data structure to another
- Displaying custom field as well as mapped fields separately - reactJS
- display questions based on user type MERN
- Async run function after useEffect
- Argument of type 'Promise<any>' is not assignable to parameter of type 'string'
- Run axe-core tests in a Create-React-App test suite
- React: How to solve: Property 'children' does not exist on type 'IntrinsicAttributes & Props'
- parameter for reducer will not update at dispatch
- Correct way to create event handlers using hooks in React?
- VSCode adds spaces inside HTML tags inside React code
- React file upload Component with jquery blueimp file upload plugin
- TypeError: items.map is not a function , how to solve this error
- Why I`m getting NaN ? Application seems to work but starting point is NaN. How can I solve this?
- Type Property Relying on Return Type of Another Property
- Styling(tailwind/SCSS stylesheet) not getting applied to dynamically inserted HTML in React
- Update state only on OK, revert back to previous state on CANCEL
- Read properties of a script which is returned when my page is loaded
- Div not showing but numbers in React
- How to filter and delete an item by using redux toolkit
- How to get children type in react
- Get the image from the server
- How to load highcharts annotations module in react typescript?
- React lazy load expensive material-ui modal
- How to connect local private geth nodes to the web page without using metamask or mist?
- State is not updating in useEffect why?
- ESLint error Unexpected block statement surrounding arrow body
- Using Nextjs Link with MUI Link or MUI Button in another (functional) component (types)
- How to install php v8js on Mac?
- Get router value from url in react