一、SpringBoot跨域配置不允許
默認情況下,SpringBoot應用程序不允許跨域訪問,這可以有效地保護應用程序數據的安全性。如果你想啟用跨域訪問,你需要進行相應的配置。
二、SpringBoot配置類
在SpringBoot中,我們可以通過創建一個配置類來處理跨域請求。具體步驟如下:
- 創建一個類,並使用註解 @Configuration。
- 在類中創建一個方法,在方法上面使用註解 @Bean,並返回一個類型為 WebMvcConfigurer 的對象。
- 在 WebMvcConfigurer 中配置跨域請求的相關參數,例如允許訪問的域名、允許的方法、允許的頭信息等。
下面是示例代碼:
@Configuration public class CorsConfig { @Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurerAdapter() { @Override public void addCorsMappings(CorsRegistry registry) { super.addCorsMappings(registry); registry.addMapping("/**") .allowedOrigins("*") .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") .allowedHeaders("*") .allowCredentials(true) .maxAge(3600); } }; } }
三、SpringBoot跨域配置不生效
如果你的配置無效,可能是你的配置被其他高優先級的配置覆蓋了。這時,你可以嘗試在應用程序的配置文件(application.properties 或 application.yml)中添加以下參數:
spring.mvc.dispatch-options-request=true
四、SpringBoot跨域配置文件在哪
SpringBoot的跨域配置文件可以放在 application.properties 或 application.yml 中,具體可以根據你的需要進行選擇。
下面是示例代碼:
# application.properties spring.mvc.dispatch-options-request=true spring.mvc.cors.allow-credentials=true spring.mvc.cors.allowed-origins=* spring.mvc.cors.allowed-methods=GET, POST, PUT, DELETE, OPTIONS spring.mvc.cors.allowed-headers=* spring.mvc.cors.max-age=3600
五、SpringBoot跨域配置無效
如果你的跨域配置依然無效,可能是你的請求沒有通過 Spring MVC 的控制器進行處理,而是通過其他方式進行處理。這時你可以使用過濾器來進行配置,具體可以參考以下代碼:
public class CorsFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; String origin = request.getHeader("Origin"); if (StringUtils.isNotBlank(origin)) { response.setHeader("Access-Control-Allow-Origin", origin); response.setHeader("Access-Control-Allow-Credentials", "true"); response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); response.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With"); response.setHeader("Access-Control-Max-Age", "3600"); } filterChain.doFilter(request, response); } @Override public void destroy() { } }
六、SpringBoot配置SSL
如果你的應用程序使用了SSL連接,你需要進行相應的配置才能正確地處理跨域請求:
# application.properties server.port=443 server.ssl.enabled=true server.ssl.key-store=classpath:keystore.p12 server.ssl.key-store-password=123456 server.ssl.key-store-type=PKCS12 server.ssl.key-alias=tomcat
七、SpringBoot跨域配置未生效
如果你的跨域配置依然未生效,可能是你的應用程序正在使用 Spring Security 進行安全控制,這時你需要在配置類中添加如下代碼:
@Configuration public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.cors().and().csrf().disable(); } }
八、SpringBoot跨域實現方式
目前,SpringBoot實現跨域請求的方式有以下幾種:
- 配置類方式
- 過濾器方式
- 使用註解方式(@CrossOrigin)
- 使用 WebSecurityConfigurerAdapter 配置方式
以上實現方式都可以有效地解決跨域請求的問題,但具體使用哪種方式,需要根據自己的需求進行選擇,並且需要注意優先級問題。
原創文章,作者:MVOS,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/136403.html