栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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已不推荐
    

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

}

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/281984.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号