Java后端如何配置跨域


在springboot项目中如何设置跨域,其实很简单,只需要在Java项目中加一个配置类即可

可以用这种配置类

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 WebConfig implements WebMvcConfigurer {
    @Value("${url}")
    private String url;
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")//设置允许跨域的路径
            	//如果有多个路径需要跨域,只需要将跨域路径放入数组中
            	//String []  allowDomain={"http://**","http://*","http://*"};
            	//.allowedOrigins(allowDomain)//多url跨域
                .allowedOrigins(url)//设置允许跨域请求的域名
                .allowCredentials(true)//是否允许证书 不写默认开启
                .allowedMethods("GET","POST","PUT","OPTIONS","DELETE","PATCH") //设置允许的方法
                .maxAge(3600);//跨域允许时间
    }
}

也可以用这种

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
@Slf4j
public class GlobalCorsConfig {
    @Value("${url}")
    private String url; //这里是从配置文件中读取
    @Bean
    public CorsFilter corsFilter() {
        log.info(this.allowedOrigin);
        //1.添加CORS配置信息
        CorsConfiguration config = new CorsConfiguration();
        //1) 允许的域,不要写*,否则cookie就无法使用了
        config.addAllowedOrigin("*"); //这里填写请求的前端服务器
        //2) 是否发送Cookie信息
        config.setAllowCredentials(true);
        //3) 允许的请求方式
        config.addAllowedMethod("OPTIONS");
        config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("DELETE");
        config.addAllowedMethod("PATCH");
        // 4)允许的头信息
        config.addAllowedHeader("*");

        //2.添加映射路径,我们拦截一切请求
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**", config);

        //3.返回新的CorsFilter.
        return new CorsFilter(configSource);
    }
}

二选一

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
后端配置跨域,可以通过 Spring Boot 和 Vue.js 的组合进行实现。 下面是一个简单的 Spring Boot 配置跨域的示例: ```java @Configuration public class CorsConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowedMethods("*") .allowedHeaders("*") .allowCredentials(true) .maxAge(3600); } } ``` 上述代码中,`@Configuration` 注解表示这是一个配置类,`CorsConfig` 类实现了 `WebMvcConfigurer` 接口。在 `addCorsMappings` 方法中,通过调用 `CorsRegistry` 对象的方法来配置跨域。 具体来说,`.addMapping("/**")` 表示配置所有的请求都支持跨域访问,`.allowedOrigins("*")` 表示所有的来源都可以访问,`.allowedMethods("*")` 表示所有的方法都可以访问,`.allowedHeaders("*")` 表示所有的头信息都可以访问,`.allowCredentials(true)` 表示允许发送 Cookie,`.maxAge(3600)` 表示缓存时间为 3600 秒。 在 Vue.js 中,可以通过在 `vue.config.js` 文件中配置代理来实现跨域。 以下是一个示例代码: ```javascript module.exports = { devServer: { proxy: { '/api': { target: 'http://localhost:8080', changeOrigin: true, pathRewrite: { '^/api': '' } } } } } ``` 以上代码中,`'/api'` 表示需要代理的路径,`target: 'http://localhost:8080'` 表示代理到的地址,`changeOrigin: true` 表示开启跨域,`pathRewrite` 表示重写路径。在 Vue.js 中发送请求时,只需要将请求路径的前缀改为 `/api` 即可,例如 `/api/user`。这样就可以实现跨域访问了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值