浅谈spring-boot 允许接口跨域并实现拦截(CORS)
程序员文章站
2024-02-20 13:17:22
本文介绍了spring-boot 允许接口跨域并实现拦截(cors),分享给大家,也给自己留个笔记
pom.xml(依赖的jar)
// 在spring-b...
本文介绍了spring-boot 允许接口跨域并实现拦截(cors),分享给大家,也给自己留个笔记
pom.xml(依赖的jar)
// 在spring-boot-starter-web的启动器中,已经依赖好了 <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency>
cors跨域的配置(主要配置允许什么样的方法跨域)
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; import org.springframework.web.servlet.config.annotation.corsregistry; import org.springframework.web.servlet.config.annotation.webmvcconfigureradapter; import java.util.arraylist; import java.util.list; /** * created by msater zg on 2017/4/3. */ @configuration public class corsconfig extends webmvcconfigureradapter { @override public void addcorsmappings(corsregistry registry) { registry.addmapping("/**") .allowedorigins("*") .allowcredentials(true) .allowedmethods("get", "post", "delete", "put") .maxage(3600); } private corsconfiguration buildconfig() { corsconfiguration corsconfiguration = new corsconfiguration(); list<string> list = new arraylist<>(); list.add("*"); corsconfiguration.setallowedorigins(list); /* // 请求常用的三种配置,*代表允许所有,当时你也可以自定义属性(比如header只能带什么,只能是post方式等等) */ corsconfiguration.addallowedorigin("*"); corsconfiguration.addallowedheader("*"); corsconfiguration.addallowedmethod("*"); return corsconfiguration; } @bean public corsfilter corsfilter() { urlbasedcorsconfigurationsource source = new urlbasedcorsconfigurationsource(); source.registercorsconfiguration("/**", buildconfig()); return new corsfilter(source); } }
拦截器配置(可以根据不同路径,配置不同的拦截器)
import org.springframework.web.servlet.handlerinterceptor; import org.springframework.web.servlet.modelandview; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; /** * created by msater zg on 2017/4/5. * 拦截器 */ public class apiinterceptor implements handlerinterceptor { @override public boolean prehandle(httpservletrequest request, httpservletresponse response, object handler) throws exception { // 请求前调用 system.out.println("拦截了"); return true; } @override public void posthandle(httpservletrequest request, httpservletresponse response, object handler, modelandview modelandview) throws exception { // 请求过程中调用 system.out.println("拦截了"); } @override public void aftercompletion(httpservletrequest request, httpservletresponse response, object handler, exception ex) throws exception { // 请求完成时调用 system.out.println("拦截了"); } }
拦截器管理类,用于生成项目的拦截器链
import org.springframework.context.annotation.configuration; import org.springframework.web.servlet.config.annotation.interceptorregistry; import org.springframework.web.servlet.config.annotation.webmvcconfigureradapter; /** * created by msater zg on 2017/4/5. * 拦截器管理工具 */ @configuration public class mywebappconfigurer extends webmvcconfigureradapter { @override public void addinterceptors(interceptorregistry registry) { // 多个拦截器组成一个拦截器链 // addpathpatterns 用于添加拦截规则 // excludepathpatterns 用户排除拦截 registry.addinterceptor(new apiinterceptor()).addpathpatterns("/user/**"); //对来自/user/** 这个链接来的请求进行拦截 super.addinterceptors(registry); } }
结语
实现跨域的方式有很多,这只是其中一种。有什么不对的地方希望能及时指出。谢谢!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。