You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法通过Python及Curl访问Spring Cloud Config服务的问题

Spring Cloud Config Server认证异常:仅浏览器可访问,curl/Python客户端无法获取配置

我搭建了一个Spring Cloud Config Server,目前包含两个yaml文件:

  • application.yml
  • application-dev.yml

已配置基于用户名密码的Spring Security基础认证,但出现异常:只有Chrome和Safari浏览器能正常访问配置信息,curl命令、Python脚本(requests库或spring-config-client模块)均无法获取配置。


配置文件内容

application.yml

# Basic configs - Name & Profile
spring:
    application:
        name: EMP_CNF-S
    profiles:
      default: ${ENVIRONMENT}

# GitHub Repository (private & SSH)
    cloud:
        config:
            server:
                git:
                    uri: git@github.com:*******/EMP_CNF-S.git
                    ignore-local-ssh-settings: true
                    default-label: ${ENVIRONMENT}
                    private-key: ${GIT_SSH_KEY}
                    search-paths: src/main/resources
                    skip-ssl-validation: true
                    timeout: 10
                    clone-on-start: true

    security:
        user:
            name: ${EMP_CONFIG_USERNAME}
            password: ${EMP_CONFIG_PASSWORD}

# Configuring Port
server:
    port: 8888

# Spring Actuator
management:
    endpoints:
        web:
            exposure:
              include:
                - health
                - info
            base-path: /actuator
    endpoint:
        health:
            show-details: always

application-dev.yml

(所有变量值已通过GitHub Actions和GitHub Secrets设置)

# Common Configurations - Datasource
 spring:
  datasource:
    url: jdbc:mysql://localhost:3306/
    username: root
    password: *******

  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

  test:
    database:
      replace: none

# Location Service
  location-service:
    schema: location_db

# Employee Service
  employee-service:
    schema: employee_db

# Customer Service
  customer-service:
    schema: ${EMP_SCHEMA_CUSTOMER}

# Supplier Service
  supplier-service:
    schema: ${EMP_SCHEMA_SUPPLIER}

测试情况

尝试访问location-service的dev环境配置时失败:

curl命令测试

执行命令:

curl -u admin:****** http://localhost:8888/location-service/dev

无法获取预期配置结果。

Python requests库测试

代码片段:

import requests
from requests.auth import HTTPBasicAuth

url = "http://localhost:8888/location-service/dev"
response = requests.get(url=url, auth=HTTPBasicAuth(username='admin', password='******'), headers=headers)

# Print the raw content
print(f"Response status code: {response.status_code}")
print(f"Response text: {response.text}")

# Attempt to parse as JSON
try:
    config = response.json()
    print(config)
except requests.exceptions.JSONDecodeError as e:
    print(f"JSON decode error: {e}")

输出结果:

Response status code: 200
Response text: <!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>Please sign in</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
    <link href="https://getbootstrap.com/docs/4.0/examples/signin/signin.css" rel="stylesheet" integrity="sha384-oOE/3m0LUMPub4kaC09mrdEhIc+e3exm4xOGxAmuFXhBNF4hcg/6MiAXAf5p0P56" crossorigin="anonymous"/>
  </head>
  <body>
     <div class="container">
      <form class="form-signin" method="post" action="/login">
        <h2 class="form-signin-heading">Please sign in</h2>
        <p>
          <label for="username" class="sr-only">Username</label>
          <input type="text" id="username" name="username" class="form-control" placeholder="Username" required autofocus>
        </p>
        <p>
          <label for="password" class="sr-only">Password</label>
          <input type="password" id="password" name="password" class="form-control" placeholder="Password" required>
        </p>
        <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
      </form>
</div>
</body></html>
JSON decode error: Expecting value: line 1 column 1 (char 0)

Python spring-config-client模块测试

代码片段:

from spring_config import ClientConfigurationBuilder
from spring_config.client import SpringConfigClient

spring_config_username = os.getenv(key = 'EMP_CONFIG_USERNAME')
spring_config_password = '****'

# Build the client configuration
config = (
    ClientConfigurationBuilder()
    .app_name("location-service")               
    .profile("dev")
    .branch('dev')
    .address("http://localhost:8888")
    .authentication((spring_config_username, spring_config_password))  # Set the authentication
    .build()
)

# Create the Spring Config Client
client = SpringConfigClient(config)

错误输出:

File "/Users/acharrade/.pyenv/versions/3.12.2/lib/python3.12/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

内容的提问来源于stack exchange,提问作者AbdoCherry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 07:59:54