public class Config implements WebMvcConfigurer
WebMvcConfigurer is not deprecated.
I produced the usable code following your question:
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 Config implements WebMvcConfigurer {
@Value("${allowed-origins:*}")
String allowedOriginsPattern;
// contains "http://localhost:8080,http://127.0.0.1:8080,http://[::1]:8080"
@Override
public void addCorsMappings(CorsRegistry registry) {
String[] origins = allowedOriginsPattern.split(",");
registry.addMapping("/**")
.allowedOriginPatterns(origins)
.allowedMethods("GET", "OPTIONS", "POST")
.allowCredentials(true);
}
}
In the application.yml, I added:
server:
servlet:
context-path: /
allowed-origins: "http://localhost:8080,http://127.0.0.1:8080,http://[::1]:8080"
An incorrect context-path
could be a reason for a CORS problem.
The example repository was here.
To try, after starting Spring Boot, please visit in your browser:
http://localhost:8080
http://127.0.0.1:8080
http://[::1]:8080
Example output in the browser:
CORS
GET http://127.0.0.1:8080/api
GET request page
POST http://127.0.0.1:8080/api
POST request page
GET http://localhost:8080/api
GET request page
POST http://localhost:8080/api
POST request page
GET http://[::1]:8080/api
GET request page
POST http://[::1]:8080/api
POST request page
In the example, the JavaScript I used:
<script>
async function getter(uri, elementId){
const responseGet = fetch(uri, { "method": "GET" })
.then(response => {
if (!response.ok) {
return response.text()
.catch(() => {
throw new Error(response.status);
})
.then(({message}) => {
console.log(message);
throw new Error(message || response.status);
});
}
return response.text();
});
const responseGetText = await Promise.resolve(responseGet);
const elementGet = document.getElementById(elementId);
elementGet.innerHTML = elementGet.innerHTML + "<div>" + responseGetText + "</div>"
}
async function poster(uri, elementId, body){
const responsePost = fetch(uri, { "method": "POST", body: body })
.then(response => {
if (!response.ok) {
return response.text()
.catch(() => {
throw new Error(response.status);
})
.then(({message}) => {
console.log(message);
throw new Error(message || response.status);
});
}
return response.text();
});
const responsePostText = await Promise.resolve(responsePost);
const elementPost = document.getElementById(elementId);
elementPost.innerHTML = elementPost.innerHTML + "<div>" + responsePostText + "</div>"
}
async function main(){
await getter("http://127.0.0.1:8080/api", "getipv4");
await poster("http://127.0.0.1:8080/api", "postipv4", JSON.stringify({"user": "user"}));
await getter("http://localhost:8080/api", "getlocalhost");
await poster("http://localhost:8080/api", "postlocalhost", JSON.stringify({"user": "user"}));
await getter("http://[::1]:8080/api", "getipv6");
await poster("http://[::1]:8080/api", "postipv6", JSON.stringify({"user": "user"}));
}
main();
</script>