浅谈spring-boot允许接⼝跨域并实现拦截(CORS)本⽂介绍了spring-boot 允许接⼝跨域并实现拦截(CORS),分享给⼤家,也给⾃⼰留个笔记
// 在spring-boot-starter-web的启动器中,已经依赖好了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
CORS跨域的配置(主要配置允许什么样的⽅法跨域)
import t.annotation.Bean;
import t.annotation.Configuration;
import org.s.CorsConfiguration;
import org.s.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.fig.annotation.CorsRegistry;
import org.springframework.fig.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();
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
springboot和过滤器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 t.annotation.Configuration;
import org.springframework.fig.annotation.InterceptorRegistry;
import org.springframework.fig.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);
}
}
结语
实现跨域的⽅式有很多,这只是其中⼀种。有什么不对的地⽅希望能及时指出。谢谢!
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论