本文介紹了spring-boot 允許接口跨域并實(shí)現(xiàn)攔截(CORS),分享給大家,也給自己留個(gè)筆記
pom.xml(依賴(lài)的jar)
1
2
3
4
5
|
// 在spring-boot-starter-web的啟動(dòng)器中,已經(jīng)依賴(lài)好了 < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > |
CORS跨域的配置(主要配置允許什么樣的方法跨域)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
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ù)不同路徑,配置不同的攔截器)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
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)求過(guò)程中調(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( "攔截了" ); } } |
攔截器管理類(lèi),用于生成項(xiàng)目的攔截器鏈
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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 用戶(hù)排除攔截 registry.addInterceptor( new ApiInterceptor()).addPathPatterns( "/user/**" ); //對(duì)來(lái)自/user/** 這個(gè)鏈接來(lái)的請(qǐng)求進(jìn)行攔截 super .addInterceptors(registry); } } |
結(jié)語(yǔ)
實(shí)現(xiàn)跨域的方式有很多,這只是其中一種。有什么不對(duì)的地方希望能及時(shí)指出。謝謝!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.jianshu.com/p/db0d8d45c242?utm_source=tuicool&utm_medium=referral