您好,登錄后才能下訂單哦!
springboot中怎么解決跨域問(wèn)題,相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
第一種:是在每個(gè)Controller里,加上注解:@CrossOrigin
import javax.validation.Valid; @CrossOrigin @RestController @RequestMapping("/user") public class UserController{
也可以在方法上加上,比如這樣,這樣針對(duì)具體的方法
@CrossOrigin @ApiOperation(value = "用戶登錄",notes = "") @PostMapping("/loginOn") public ResponseMessage loginOn(@RequestBody @Valid UserReq userReq){
每一個(gè)Controller這樣寫也是很麻煩。
第二種:是實(shí)現(xiàn)WebMvcConfigurer接口,在接口中進(jìn)行跨域支持
以前可以繼承WebMvcConfigurerAdapter,springboot2.x版本已經(jīng)將其@Deprecated
我們直接實(shí)現(xiàn)接口:
@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); }
但使用這種方法,我今天遇到一個(gè)坑,我準(zhǔn)備在攔截器里面對(duì)用戶的請(qǐng)求進(jìn)行攔截
@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("沒(méi)有權(quán)限,請(qǐng)先登錄!"); }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 是自定義的統(tǒng)一的響應(yīng)信息類:
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); } }
通過(guò)這樣的處理發(fā)現(xiàn),前端一直報(bào)跨域異常問(wèn)題,這時(shí)候有了第三種方法
第三種:使用CorsFilter過(guò)濾器:
寫一個(gè)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)); //設(shè)置過(guò)濾器的順序 bean.setOrder(0); return new CorsFilter(source); } }
看完上述內(nèi)容,你們掌握springboot中怎么解決跨域問(wèn)題的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。