跨域设置

 // 设置springboot的跨域访问

@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
      // 设置允许跨域的路径
        registry.addMapping("/**")
                // 设置允许跨域请求的域名
                .allowedOriginPatterns("*")
                // 是否允许cookie
                .allowCredentials(true)
                // 设置允许的请求方式
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                // 设置允许的header属性
                .allowedHeaders("*")
                // 跨域允许时间
                .maxAge(3600);
    }
}



// 设置spring-security的跨域访问

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true) // 开启授权验证
public class SecurityConfig extends WebSecurityConfigurerAdapter {
            @Override
    protected void configure(HttpSecurity http) throws Exception {
       
        // 允许跨域
        http.cors();
    }
}

  

posted @ 2022-04-12 15:31  scwyfy  阅读(134)  评论(0)    收藏  举报