簡體   English   中英

如何阻止從 Postman/其他 API 的/等訪問我的 API .. (Spring Boot)

[英]How to block access to my API from Postman/Other API's/etc.. (Spring Boot)

我正在使用 Spring 引導和 Z38008DD81C2F4D798Z5ECF6 我有公共和私人區域,我使用 Spring Security 進行身份驗證(用於私人區域)。

The problem is that i configured CORS and it blocks requests if i call public endpoints from unauthorized url's but and i was surprised that if i call it from Postman or another Spring Boot App using RestTemplate, the CORS don't block the request and return the結果。

我在 Internet 上讀到 CORS 僅阻止來自瀏覽器的調用。那么如何保護我的 API 的公共部分不從 Postman 或其他 API 調用它?

@Bean
    CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Arrays.asList("http://localhost:4200","http://localhost:4201"));
        configuration.setAllowedMethods(Arrays.asList("GET","POST","DELETE"));
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }

恐怕沒有解決辦法。 在 Postman 中,您可以添加所需的任何標題。 因此,如果您擁有所有必要的令牌,則可以模仿任何客戶。 此外,CORS 的用途略有不同:

CORS 的用例很簡單。 想象一下站點 alice.com 有一些站點 bob.com 想要訪問的數據。 在瀏覽器的同源策略下,這種類型的請求傳統上是不允許的。 但是,通過支持 CORS 請求,alice.com 可以添加一些特殊的響應頭,允許 bob.com 訪問數據。

您可以在此處找到更多信息: https://medium.com/@baphemot/understanding-cors-18ad6b478e2b

private Map<String, String> getRequestHeadersInMap(HttpServletRequest request) {

    Map<String, String> result = new HashMap<>();

    Enumeration headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String key = (String) headerNames.nextElement();
        String value = request.getHeader(key);
        result.put(key, value);
    }

    return result;
}

暫無
暫無

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

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