springmvc拦截器拦截token失效出现跨域问题

拦截器preHandle返回false时,前端出现跨域问题

在preHandle 返回false时,http就中断了请求,我们在WebMvcConfigurer的addCrosMappings中配置的跨域问题解决方案就无法执行,所以我们在拦截器中preHandle中return false前要给response近行配置:

        String origin = request.getHeader("Origin");
        response.addHeader("Access-Control-Allow-Origin", origin);
        response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
        response.addHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
        response.addHeader("Access-Control-Allow-Credentials", "true");
        response.addHeader("Access-Control-Max-Age", "3600");

还有一种方法就是通过RestControllerAdvice拦截异常并进行统一处理,自定义异常,并抛出异常

你可能感兴趣的:(springmvc跨域问题)