【问题标题】:gaining access to a secured SwaggerResource获得对安全的 SwaggerResource 的访问权限
【发布时间】:2019-12-16 10:20:23
【问题描述】:

我一直在研究这个例子https://github.com/varghgeorge/microservices-single-swagger,我遇到的问题是我知道我的swagger文档的端点但是为了直接通过我的浏览器访问它,我需要提供用户名和密码。我想以编程方式执行此操作以避免出现以下错误。

作为配置提供后,url 带有附加的访问凭据:

Errors
Hide
Fetch errorFailed to execute 'fetch' on 'Window': Request cannot be constructed from a URL that includes credentials: https://username:password@my-host/v2/api-docs https://username:password@my-host/v2/api-docs
Fetch errorPossible cross-origin (CORS) issue? The URL origin (https://my-host) does not match the page (http://localhost:8080). Check the server returns the correct 'Access-Control-Allow-*' headers.

【问题讨论】:

    标签: authentication swagger springfox


    【解决方案1】:

    我遇到了同样的问题,我在 API 项目的安全配置中添加了一个新的 @Bean 解决了它

        @Bean
        public CorsFilter corsFilter() {
            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    
            CorsConfiguration config = new CorsConfiguration().applyPermitDefaultValues();
            config.setAllowCredentials(false);
            source.registerCorsConfiguration("/v2/api-docs", config);
    
            return new CorsFilter(source);
        }
    

    Springfox 文档中提出了一个非常相似的代码来启用 CORS 标头。 See here.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-15
      • 1970-01-01
      • 2011-06-03
      • 2016-03-27
      相关资源
      最近更新 更多