java攔截器跨域報錯$PreFlightHandler cannot be cast to class org.springframework.web.method.HandlerMethod


1、問題描述

作者在寫springboot項目時發生了這個樣一個錯誤,在非跨域請求測試中沒有出現,但是在跨域請求測試時出現了問題( ... has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.)。

錯誤提示

報錯內容是攔截器中的這句話、hander的類型轉化錯誤。

HandlerMethod handlerMethod = (HandlerMethod) handler;

2、問題的解決:

這里只需要在這句話前加入一個判斷就好

if (!(handler instanceof HandlerMethod)) {
    return false;
}
HandlerMethod handlerMethod = (HandlerMethod) handler;

3、問題原因的猜想

對於json傳輸的跨域是一個復雜跨域,可以在前端控制台看到進行了兩次請求,一次是"預請求"用於判斷是否支持跨域,一次是正式傳輸數據的請求。
兩次請求

可以看見有一次請求返回了500,返回500的應該是跨域的"預請求",當他返回失敗之后,帶有參數請求應該中斷。果然,另一個請求沒有返回。所以作者推測是handler在“預請求”中不能被轉化為HandlerMethod。所以只需要單獨判斷一下handler能不能進行類型轉化就行了。

4、配置跨域

都寫到這里了,順便把跨域的配置也記錄一下,作者的springboot版本是2.3.1,測試使用攔截器失敗了,最后使用了過濾器終結了跨域的配置。

package com.xxx.demo.config;

import org.springframework.context.annotation.Configuration;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "CorsFilter ")
@Configuration
public class CorsFilter implements Filter {
    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin","*"); // 允許的來源
        response.setHeader("Access-Control-Allow-Credentials", "true"); // 是否允許證書
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, PATCH, DELETE, PUT"); // 允許的請求方式
        response.setHeader("Access-Control-Max-Age", "3600"); // 預檢請求的有效期
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        chain.doFilter(req, res);
    }
}

參考:
https://segmentfault.com/a/1190000019550329?utm_source=tag-newest

https://www.cnblogs.com/qunxiadexiaoxiangjiao/p/9446956.html

結語:跨域爸爸放過我吧。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM