簡體   English   中英

在運行時將 CORS 映射動態添加到 Rest Controller

[英]Dynamically add CORS Mappings to Rest Controller at runtime

嗨! 我是 Spring Boot 的初學者。 我有一個使用 Rest 控制器的簡單 Java Spring Boot 應用程序。 一些控制器的端點暴露給 CORS 請求。 我發現可以通過在方法上方添加 @CrossOrigin(origins ="..") 注釋來允許 CORS 請求。 我的問題是我目前不知道所有的起源,並且必須在我的 Spring Boot 應用程序運行時稍后添加其中的一些。 是否可以使用一種方法將新的源地址手動添加到我的 Rest Controller 的某些端點? 例如,在我的 Rest Controller 中有一個端點,允許特定用戶(例如管理員)將新的 cors-origins 添加到 rest 端點? 如果是:請舉一個簡單的例子。

我認為這可以通過實現你自己的org.springframework.web.cors.CorsProcessor 查看org.springframework.web.filter.CorsFilter實現,它可以通過public void setCorsProcessor(CorsProcessor processor)方法使用您的自定義CorsProcessor進行配置。

然后我會建議實現您自己的CorsProcessor並在方法中process您的自定義規則。

或者另一種選擇是在您的 Spring Security Config 中設置您自己的CorsConfigurationSource 例如

@Override
 protected void configure(HttpSecurity http) throws Exception {
        http.cors().configurationSource(new CustomConfigSource());
}

CorsConfigurationSource提供了一個方法CorsConfiguration getCorsConfiguration(HttpServletRequest request); 您可以在其中處理特定的 CORS 處理。

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    response.addHeader("Access-Control-Allow-Origin", "*");

    if (request.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equals(request.getMethod())) {
        LOG.trace("Sending Header....");
        // CORS "pre-flight" request
        response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");

        response.addHeader("Access-Control-Allow-Headers", "Content-Type");
        response.addHeader("Access-Control-Max-Age", "1");
    }

    filterChain.doFilter(request, response);
}

請使用過濾器cros_filter

以下在 Spring 2.4 中對我有用:

import java.util.Arrays;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration corsConfiguration = new CorsConfiguration();

        final String corsAllowedOrigins = System.getenv("CORS_ALLOWED_ORIGINS");

        corsConfiguration.setAllowCredentials(true);
        corsConfiguration.setAllowedOrigins(Arrays.asList(corsAllowedOrigins.split(",")));
        corsConfiguration.setAllowedMethods(Arrays.asList(CorsConfiguration.ALL));
        corsConfiguration.setAllowedHeaders(Arrays.asList(CorsConfiguration.ALL));

        source.registerCorsConfiguration("/**", corsConfiguration);

        return new CorsFilter(source);
    }
}

您可以通過res.addHeader("Access-Control-Allow-Origin","what you want here like http/.....");直接將其添加到 HttpServletResponse res 中res.addHeader("Access-Control-Allow-Origin","what you want here like http/.....");

@RequestMapping(value="/getbot/{id}", 
       method = RequestMethod.POST,
       headers="Accept=application/json") 
@ResponseBody public String getbot(HttpServletResponse res) { 
          res.addHeader("Access-Control-Allow-Origin","*");
          ...
}

我試過用 springboot 沒有辦法做到這一點,因為有默認的 cors 處理程序會覆蓋你的頭文件,我沒有找到阻止它的方法我認為我們需要為那個 springboot 編寫 servlet 我們不能動態地做到這一點

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM