Spring Cloud Config 基本安全抛出 401 错误

问题描述 投票:0回答:4

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

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?

spring-cloud-config
4个回答
1
投票

尝试检查这些配置:

  • spring.cloud.config.用户名
  • spring.cloud.config.password

这两个属性都应该在 bootstrap.properties (而不是 application.properties)中定义


0
投票

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

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

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

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


0
投票

是的,我们必须使用引导属性,因为当 Spring Cloud 应用程序启动时,它会创建一个引导上下文。引导上下文正在搜索 bootstrap.properties 或 bootstrap.yaml 文件,而应用程序上下文正在搜索 application.properties 或 application.yaml 文件。引导上下文是主应用程序的父上下文


0
投票

如果您使用的是 Spring Security,请检查 websecurityconfig.java 或任何其他类似文件。将 PermitAll() 添加到您尝试与之通信的 api 路径。

© www.soinside.com 2019 - 2024. All rights reserved.