spring-cloud-config - Spring Cloud Config Basic Security 抛出 401 错误

标签 spring-cloud-config

我在服务器端有以下配置:

server:
  port: 8888

spring:
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          search-locations: "classpath:/config"
  security:
    user:
      name: test
      password: test

并在客户端进行以下配置:
spring:
  cloud:
    config:
      fail-fast: true
      profile: "${spring.profiles.active}"
      uri: "${SPRING_CLOUD_CONFIG_URI:http://localhost:8888/}"
      username: test
      password: test

我可以使用 user/pwd 从浏览器成功访问属性如 test/test ,但是当我的客户端尝试获取它时失败并出现 401 错误:
INFO 7620 --- [5cee934b64bfd92] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
WARN 7620 --- [5cee934b64bfd92] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: 401 null

我尝试将 spring cloud 的日志级别设置为 DEBUG,但没有记录任何其他内容,所以我不知道为什么我从客户端获取 401,而我可以使用相同的凭据通过浏览器成功访问属性。

我还尝试从服务器和客户端中删除安全性,它运行得很好,这意味着其余的配置都很好。但问题是,当我应用基本安全性时,我忽略了什么,为什么它不起作用并抛出 401?

最佳答案

请检查您指定配置文件名称的方式是否正确,以及它是否在 Java 代码中正确解析。您可以实现 CommandLineRunner 并从 Environment 变量打印事件配置文件。

如果您指定了属性 spring.profiles.active 作为 pom.xml 中的 native ,您可以在 application/yaml 文件中将其解析为 @spring.profiles.active@

如果您将属性文件指定为 VM 参数,则它应该适用于当前实现。

如果您没有在 pom 或 VM 参数中指定 spring.profiles.active,它将解析为默认配置文件,而不是 native 配置文件。配置客户端和配置服务器中的配置文件应该相同。

关于spring-cloud-config - Spring Cloud Config Basic Security 抛出 401 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56336879/

相关文章:

spring-boot - 如何在 spring boot bootstrap.yml 文件中获取 docker ip 和端口

java - Spring Cloud Config 服务器显示 invalidPrivateKey

spring-boot - 使用 Spring Cloud Config 的自定义属性加载器

java - 你如何摆脱 ConfigurationPropertiesRebinderAutoConfiguration INFO 日志消息

java - 在java库中使用基于环境的配置属性

spring-boot - Spring-Doc open api 无法与 Spring cloud 配置服务器 @EnableConfigServer 一起使用

git - 无法将 Spring Cloud Config Server 与远程 Git 集成

spring-boot - 配置服务器和 Eureka 服务器在同一个应用程序 : tries to connect to localhost:8761

java - 属性 'spring.cloud.config.server.git.privateKey' 不是有效的私钥

spring-boot - 如何使用 env 特定配置创建 Spring Cloud Config Client?