淺談spring-boot 允許接口跨域并實(shí)現(xiàn)攔截(CORS)
本文介紹了spring-boot 允許接口跨域并實(shí)現(xiàn)攔截(CORS),分享給大家,也給自己留個(gè)筆記
pom.xml(依賴的jar)
// 在spring-boot-starter-web的啟動(dòng)器中,已經(jīng)依賴好了
<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);
/*
// 請(qǐng)求常用的三種配置,*代表允許所有,當(dāng)時(shí)你也可以自定義屬性(比如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);
}
}
攔截器配置(可以根據(jù)不同路徑,配置不同的攔截器)
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 {
// 請(qǐng)求前調(diào)用
System.out.println("攔截了");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
// 請(qǐng)求過程中調(diào)用
System.out.println("攔截了");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
// 請(qǐng)求完成時(shí)調(diào)用
System.out.println("攔截了");
}
}
攔截器管理類,用于生成項(xiàng)目的攔截器鏈
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) {
// 多個(gè)攔截器組成一個(gè)攔截器鏈
// addPathPatterns 用于添加攔截規(guī)則
// excludePathPatterns 用戶排除攔截
registry.addInterceptor(new ApiInterceptor()).addPathPatterns("/user/**"); //對(duì)來自/user/** 這個(gè)鏈接來的請(qǐng)求進(jìn)行攔截
super.addInterceptors(registry);
}
}
結(jié)語
實(shí)現(xiàn)跨域的方式有很多,這只是其中一種。有什么不對(duì)的地方希望能及時(shí)指出。謝謝!
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Apache中配置支持CORS(跨域資源共享)實(shí)例
- JS跨域解決方案之使用CORS實(shí)現(xiàn)跨域
- js實(shí)現(xiàn)跨域的幾種方法匯總(圖片ping、JSONP和CORS)
- Node.js設(shè)置CORS跨域請(qǐng)求中多域名白名單的方法
- vue+springboot實(shí)現(xiàn)項(xiàng)目的CORS跨域請(qǐng)求
- 淺談Koa2框架利用CORS完成跨域ajax請(qǐng)求
- 跨域解決之JSONP和CORS的詳細(xì)介紹
- C# WebApi CORS跨域問題解決方案
- react中fetch之cors跨域請(qǐng)求的實(shí)現(xiàn)方法
- 跨域(CORS)問題的解決方案分享
相關(guān)文章
springboot中設(shè)置定時(shí)任務(wù)的三種方法小結(jié)
在我們開發(fā)項(xiàng)目過程中,經(jīng)常需要定時(shí)任務(wù)來幫助我們來做一些內(nèi)容,本文介紹了springboot中設(shè)置定時(shí)任務(wù)的三種方法,主要包括@Scheduled注解,Quartz框架和xxl-job框架的實(shí)現(xiàn),感興趣的可以了解一下2023-12-12
Java中雙冒號(hào)運(yùn)算符(::)的用法詳解
在Java 8引入的Lambda表達(dá)式和函數(shù)式接口之后,雙冒號(hào)運(yùn)算符(::)成為了一項(xiàng)重要的功能,下面我們就來學(xué)習(xí)一下Java中的雙冒號(hào)運(yùn)算符及其常見應(yīng)用場景吧2023-12-12
SpringMVC Tomcat控制臺(tái)亂碼問題解決方案
這篇文章主要介紹了SpringMVC Tomcat控制臺(tái)亂碼問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
在Java的Spring框架的程序中使用JDBC API操作數(shù)據(jù)庫
這篇文章主要介紹了在Java的Spring框架的程序中使用JDBC API操作數(shù)據(jù)庫的方法,并通過示例展示了其存儲(chǔ)過程以及基本SQL語句的應(yīng)用,需要的朋友可以參考下2015-12-12
Java自動(dòng)取款機(jī)ATM案例實(shí)現(xiàn)
本文主要介紹了Java自動(dòng)取款機(jī)ATM案例實(shí)現(xiàn),整個(gè)過程可以分為三部分:登錄賬戶和執(zhí)行取款操作,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08
基于mybatis高級(jí)映射多對(duì)多查詢的實(shí)現(xiàn)
下面小編就為大家?guī)硪黄趍ybatis高級(jí)映射多對(duì)多查詢的實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
Spring Boot 使用 Swagger 構(gòu)建 RestAPI 接口文檔
這篇文章主要介紹了Spring Boot 使用 Swagger 構(gòu)建 RestAPI 接口文檔,幫助大家更好的理解和使用Spring Boot框架,感興趣的朋友可以了解下2020-10-10

