注释CrossOrigin在Spring Boot中不起作用

苏联

我有一个Spring Boot应用程序,它公开了一些端点。我想从React应用程序向这些端点发出请求,但它一直给我带来CORS问题:

CORS策略已阻止从来源' http:// localhost:3000 '访问'localhost:9090 / helios-admin / api / dashboard / clients?page = 0&size = 30'处的XMLHttpRequest :跨来源请求仅支持协议方案:http,数据,chrome,chrome扩展名,https。

因此,我尝试@CrossOrigin对所有方法以及Controller类使用批注,但是错误是相同的。
我的应用程序中的get请求如下所示:

constructor() {
        this.url = 'localhost:9090/helios-admin/api/dashboard/clients?page=0&size=30';
    }

    getProjectStatusById(projectId) {
        Axios.get(this.url).then(res=>{
            console.log(res.data);
        })
    }

少了什么东西?

编辑在我的spring boot应用程序中,我只有此类可配置安全性:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, jsr250Enabled = true, prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {



    @Autowired
    SysdataUserDetailsService sysdataUserDetailsService;



    @Autowired
    private JwtAuthEntryPoint jwtAuthEntryPoint;



    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .userDetailsService(sysdataUserDetailsService)
                .passwordEncoder(encoder());
    }



    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable().authorizeRequests()
                .antMatchers(PathConstants.USER_AUTH +"/**", PathConstants.HELIOS+"/dashboard/**").permitAll()
                .antMatchers(HttpMethod.GET, "/"+PathConstants.PROCESS_DEFINITION+"/**").permitAll()
                .antMatchers(HttpMethod.POST, "/"+PathConstants.PROCESS_DEFINITION+"/**").permitAll()
                .antMatchers(HttpMethod.GET, "/"+PathConstants.PROCESS_INSTANCE+"/**").permitAll()
                //.anyRequest().authenticated()
                .anyRequest().permitAll()
                .and()
                .exceptionHandling().authenticationEntryPoint(jwtAuthEntryPoint).and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        // custom jwt filter.
        http.addFilterBefore(jwtAuthFilter(), UsernamePasswordAuthenticationFilter.class);
    }
}
阿卜杜勒加尼·鲁西(Abdelghani Roussi)

您可以通过重写添加它addCorsMappingsWebMvcConfigurerAdapter,所以无论是创建一个类扩展 WebMvcConfigurerAdapter或定义你这样的配置类一个bean:

    @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);
            }
        }
    }

编辑

从5.0版开始WebMvcConfigurerAdapter不推荐使用,因此您可以通过实现WebMvcConfigurer接口来实现相同的功能(添加了默认方法,感谢Java 8!可以直接实现而不需要此适配器)

@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);
   }
 }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

注释@CrossOrigin(“ *”)不起作用

事务注释在Spring Boot中不起作用

事务注释在使用JPA的Spring Boot中不起作用

@Transactional注释rollbackFor值在Spring Boot中不起作用

Spring Boot CORS设置:CrossOrigin注释无法正常运行addCorsMappings

Spring Boot @ApiController 注释不起作用

WebMvcConfigurer 在 Spring Boot 中不起作用?

Spring中的@Qualifier注释不起作用

json中的Json在Spring Boot中不起作用

Spring Boot验证注释@Valid和@NotBlank不起作用

Spring Boot-@Value注释不起作用

为什么NotNull注释不起作用Spring Boot

Spring Boot EnableCaching和可缓存注释不起作用

使用nativeQuery的Spring Boot Query注释在Postgresql中不起作用

Jax-rs 注释路径在 java spring boot 中不起作用?

@Transactional在带有CrudRepository的Spring Boot中不起作用

Spring Boot-@PreAuthorize在测试中不起作用

RestController在oauth2 Spring Boot中不起作用

Spring Boot 2.2.2 - Prometheus 在 Actuator 中不起作用

多模块组件扫描在Spring Boot中不起作用

我的BeanPostProcessor在Spring Boot中不起作用

Spring Boot - @Autowired 在 @Service 中不起作用

自定义MessageConverter在Spring Boot中不起作用

Spring Boot Kotlin项目在Eclipse中不起作用

Spring Boot DevTools在Eclipse中不起作用

Spring Boot @Scheduler 在 Google Kubernetes Engine 中不起作用

Primefaces FileUpload在Spring Boot中不起作用

spring boot中的BindingResult参数不起作用

实时重载在Spring Boot devtools中不起作用