Problem: Cross-domain problem CORS

problem

In the past, when writing the control layer code of the backend, the
frontend often encountered cross-domain problems when writing its own requests.
Why?
Open the backend service port 8080
directly from IDEA. The
static resource file html has a jquery request
. The browser found that the port is
63342. The request was intercepted by cors
Obviously 127.0.0.1:63342 and 127.0.0.1:8080 are not the same port
Hug one

Access to XMLHttpRequest at xxx orgin xxx  ... 

Cannot access across domains!

I changed that line, I changed a back-end code,
I added on the Controller


@CrossOrigin("*")

Can be solved in the simplest way

Annotated versions can be configured under Spring Boot

@Configuration
public class MyConfiguration {

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**");
                // 这样可以调整颗粒度 更加自由
                registy.addMapping("/api");
            }
        };
    }
}

xml version

<mvc:cors>
        <mvc:mapping path=""/>
</mvc:cors>

summary

Only XMLHttpRequest requests have cross-domain
The common one is the native Xml request
jquery encapsulated XmL request. The
ordinary href = "https: // xxx" does not appear

Non-homologous, non-homologous, and non-homologous protocols produce cross-domain

Published 22 original articles · Likes2 · Visits 881

Guess you like

Origin blog.csdn.net/weixin_41685373/article/details/104979014