springboot+shiro+vue前后端分离跨域问题

工程为前后端分离模式,后端为springboot+shiro、前端为vue;前端部署在nginx中;
在此前提条件为项目正常,配置正常,不再赘述。
前端调用后端接口服务时报跨域问题:Response to preflight request doesn’t pass access control check: Redirect is not allowed for a preflight request.
解决方案:

@Configuration
public class CorsConfig implements WebMvcConfigurer {

    // 此已过时,springboot2已不推荐
    /*@Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins(ALL)
                .allowedMethods(ALL)
                .allowedHeaders(ALL)
                .allowCredentials(true);
    }*/

    /**
     * @description: setMaxAge(3600L);和setAllowCredentials(true);务必加上,否则无论前端怎么设置,后端对过滤怎么重写,response请求返回状态都是302
     * @author:
     * @date: 
     */
    @Bean
    public FilterRegistrationBean corsFilter() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 允许任何域名访问
        corsConfiguration.addAllowedOrigin("*");
        // 允许任何header访问
        corsConfiguration.addAllowedHeader("*");
        // 允许任何方法访问
        corsConfiguration.addAllowedMethod("*");
        // 预检请求的有效期,单位为秒
        corsConfiguration.setMaxAge(3600L);
        // 是否支持安全证书(必需)
        corsConfiguration.setAllowCredentials(true);
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
        CorsFilter corsFilter = new CorsFilter(urlBasedCorsConfigurationSource);
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(corsFilter);
        // 设置优先级
        filterRegistrationBean.setOrder(0);
        return filterRegistrationBean;
    }

}

采用注释掉的部分,出现如上跨域问题,通过采用下面的方式解决在浏览器中访问后端接口服务出现跨域报错。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值