springboot跨域问题解决方案
这篇文章主要介绍了springboot跨域问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
springboot中的跨域问题,如果不注意的话,容易造成错误,本次springboot版本为2.13
前端错误信息:
Access to XMLHttpRequest at 'http://localhost:8080/user/loginOn' from origin 'http://localhost:8082' has been blocked by CORS policy:
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
第一种:是在每个Controller里,加上注解:@CrossOrigin
import javax.validation.Valid; @CrossOrigin @RestController @RequestMapping("/user") public class UserController{
也可以在方法上加上,比如这样,这样针对具体的方法
@CrossOrigin @ApiOperation(value = "用户登录",notes = "") @PostMapping("/loginOn") public ResponseMessage loginOn(@RequestBody @Valid UserReq userReq){
每一个Controller这样写也是很麻烦。
第二种:是实现WebMvcConfigurer接口,在接口中进行跨域支持
以前可以继承WebMvcConfigurerAdapter,springboot2.x版本已经将其@Deprecated
我们直接实现接口:
@Configuration public class WebConfig implements WebMvcConfigurer { /** * 跨域支持 * @param registry */ @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowCredentials(true) .allowedMethods("GET", "POST", "DELETE", "PUT") .maxAge(3600 * 24); }
但使用这种方法,我今天遇到一个坑,我准备在拦截器里面对用户的请求进行拦截
@Component public class RequestInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { Object loginUser = request.getSession().getAttribute("token"); if(loginUser == null){ //自定义的异常类,这里抛出异常,交给全局异常捕捉类处理 throw new ServiceException("没有权限,请先登录!"); }else{ return true; } } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }
全局异常捕捉类:
@RestControllerAdvice public class GlobleExceptionHandler { @ExceptionHandler(value = ServiceException.class) public ResponseMessage caughtException(ServiceException e){ return new ResponseMessage(e.getMsg()); } }
ResponseMessage 是自定义的统一的响应信息类:
ResponseMessage
@Data public class ResponseMessage { private Integer Code; private String msg; private Integer count; private Object data; public ResponseMessage(Object data) { this.data = data; } public ResponseMessage(String msg) { this.msg = msg; } public ResponseMessage(Integer code, String msg) { Code = code; this.msg = msg; } public ResponseMessage(Integer code, String msg, Integer count) { Code = code; this.msg = msg; this.count = count; } public ResponseMessage(Integer code, String msg, Integer count, Object data) { Code = code; this.msg = msg; this.count = count; this.data = data; } public static ResponseMessage success(String msg){ return new ResponseMessage(200,msg); } public static ResponseMessage fail(Integer code,String msg){ return new ResponseMessage(code,msg); } }
通过这样的处理发现,前端一直报跨域异常问题,这时候有了第三种方法
第三种:使用CorsFilter过滤器:
写一个MyCorsConfig 配置类
@Configuration public class MyCorsConfig { @Bean public CorsFilter corsFilter() { CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.addAllowedOrigin("*"); corsConfiguration.addAllowedHeader("*"); corsConfiguration.addAllowedMethod("*"); corsConfiguration.setAllowCredentials(true); corsConfiguration.setMaxAge(3600L); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", corsConfiguration); FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source)); //设置过滤器的顺序 bean.setOrder(0); return new CorsFilter(source); } }
最终解决本次demo的跨域问题。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
IDEA启动报错Internal error. Please refer to https://jb.gg/i
这篇文章主要介绍了IDEA启动报错Internal error. Please refer to https://jb.gg/ide/critical-startup-errors解决办法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2023-04-04Springboot 项目读取Resources目录下的文件(推荐)
这篇文章主要介绍了Springboot 项目读取Resources目录下的文件,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-11-11关于Spring的@Transaction导致数据库回滚全部生效问题(又删库跑路)
使用@Transactional一键开启声明式事务, 这就真的事务生效了?过于信任框架总有“意外惊喜”。本文通过案例给大家详解关于Spring的@Transaction导致数据库回滚全部生效问题,感兴趣的朋友一起看看吧2021-05-05Elasticsearch Join字段类型简单快速上手教程
这篇文章主要为大家介绍了Elasticsearch Join字段类型简单快速上手教程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-09-09详解eclipse创建maven项目实现动态web工程完整示例
这篇文章主要介绍了详解eclipse创建maven项目实现动态web工程完整示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-12-12使用springboot每日推送早安问候语到用户微信的全过程
近期网上又出现一股给女朋友做微信公众号推送的潮流,所以这篇文章主要给大家介绍了关于如何使用springboot每日推送早安问候语到用户微信的相关资料,文中通过图文以及实例代码介绍的非常详细,需要的朋友可以参考下2022-11-11
最新评论