org.ebaysf.web的cors-filter使用,cors-filter跨域请求

org.ebaysf.web的cors-filter使用,cors-filter跨域请求

================================

©Copyright 蕃薯耀 2020-11-26

https://www.cnblogs.com/fanshuyao/

cors-filter为第三方(ebay)组件。

一、官网地址

https://github.com/ebay/cors-filter

二、Springboot使用cors-filter

1、引入依赖

<dependency>
    <groupId>org.ebaysf.web</groupId>
    <artifactId>cors-filter</artifactId>
    <version>1.0.1</version>
</dependency>

2、配置类

import javax.servlet.Filter;

import org.ebaysf.web.cors.CORSFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 过滤器配置类
 */
@Configuration
public class HttpFilterConfig {
    
    
    /**
     * org.ebaysf.web cors-filter
     * @return
     */
    @Bean
    public FilterRegistrationBean<Filter> corsFilter() {
        FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
        
        registration.setFilter(new CORSFilter());//org.ebaysf.web.cors.CORSFilter
        
        //这个可直接不配置
        //Defaults: true
        registration.addInitParameter("cors.support.credentials", "true");
        
        //这个可直接不配置
        //Defaults: * (Any origin is allowed to access the resource).
        registration.addInitParameter("cors.allowed.origins", "http://127.0.0.1:7010");
        
        //这个可直接不配置
        //Defaults: GET,POST,HEAD,OPTIONS
        registration.addInitParameter("cors.allowed.methods", "GET,POST");
        
        //这个可直接不配置
        // Default
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误通常是因为在Spring应用程序中缺少了对CorsFilter的配置。CorsFilter是用于处理跨域资源共享(CORS)的过滤器,它可以帮助解决跨域访问的问题。 要解决这个问题,你可以按照以下步骤进行操作: 1. 确保你的Spring应用程序中已经引入了相关的依赖。在你的pom.xml(如果是Maven项目)或build.gradle(如果是Gradle项目)文件中添加以下依赖: ```xml <!-- Maven --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Gradle --> implementation 'org.springframework.boot:spring-boot-starter-web' ``` 2. 确保在你的代码中正确配置了CorsFilter。可以通过创建一个配置类并添加相应的注解来完成此操作。例如,在你的配置类上添加`@Configuration`注解,并在其中创建一个方法来配置CorsFilter: ```java @Configuration public class MyConfig { @Bean public CorsFilter corsFilter() { CorsConfiguration corsConfig = new CorsConfiguration(); // 配置跨域访问规则 // ... UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", corsConfig); return new CorsFilter(source); } } ``` 请根据你的实际需求配置跨域访问规则。 3. 确保你的代码中使用了正确的注解来启用跨域支持。可以在你的Controller类或方法上添加`@CrossOrigin`注解,或者在全局配置中启用跨域支持。 通过以上步骤,你应该能够解决"No qualifying bean of type 'org.springframework.web.filter.CorsFilter' available"错误。如果问题仍然存在,请检查你的配置是否正确,并确保依赖项已正确引入。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值