springboot解决跨域cores
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 corsconfig implements webmvcconfigurer { @override public void addcorsmappings(corsregistry registry) { registry.addmapping("/**") .allowedoriginpatterns("*") .allowedheaders("*") .allowedmethods("*") .allowcredentials(true) .maxage(3600); } }
springboot允许跨域
解决办法
@springbootapplication @mapperscan("com.humorchen.pastry_examination.mapper") public class pastryexaminationapplication implements webmvcconfigurer { public static void main(string[] args) { springapplication.run(pastryexaminationapplication.class, args); } @override public void addcorsmappings(corsregistry registry) { registry.addmapping("/**") .allowcredentials(true) .allowedheaders("*") .allowedoriginpatterns("*") .allowedmethods("*"); } }
新版本springboot跨域解决办法,把这个配置bean注入就可以了
@bean public webmvcconfigurer corsconfigurer() { return new webmvcconfigurer() { @override public void addcorsmappings(corsregistry registry) { registry.addmapping("/**") .allowedheaders("*") .allowedmethods("*") .allowedorigins("*") .allowcredentials(true); } }; }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论