我刚刚使用 Maven 创建了一个空的 Spring Boot 应用程序,具有以下依赖项:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>io.awspring.cloud</groupId>
<artifactId>spring-cloud-aws-starter-secrets-manager</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sts</artifactId>
<version>2.29.34</version>
</dependency>
</dependencies>
我有两个资源文件:
应用程序属性:
spring.application.name=aws-sts
application-local.properties:
spring.cloud.aws.secretsmanager.enabled=false
我使用以下命令启动我的应用程序:
./mvnw spring-boot:run -Dspring.profiles.active=local
然后,我收到以下错误:
...
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [software.amazon.awssdk.auth.credentials.AwsCredentialsProvider]: Factory method 'credentialsProvider' threw exception with message: Unable to load region from any of the providers in the chain software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain@6c37bd27: [software.amazon.awssdk.regions.providers.SystemSettingsRegionProvider@2b9b7f1f: Unable to load region from system settings. Region must be specified either via environment variable (AWS_REGION) or system property (aws.region)., software.amazon.awssdk.regions.providers.AwsProfileRegionProvider@60723d6a: No region provided in profile: default, software.amazon.awssdk.regions.providers.InstanceProfileRegionProvider@7a24eb3: Unable to contact EC2 metadata service.]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.lambda$instantiate$0(SimpleInstantiationStrategy.java:199) ~[spring-beans-6.2.1.jar:6.2.1]
STS 依赖项似乎需要一个区域(
spring.cloud.aws.region.static=${aws.region}
),但是,按照 Spring Cloud 存储库中本问题最后的建议,我们只需要在所需配置文件中禁用 AWS 秘密管理。我就是这么做的。
htpaf 于 2024 年 11 月 27 日 对于其他寻找此问题解决方案的人来说, 查看 awspring/spring-cloud-aws#824 和修复部分 awspring/spring-cloud-aws#1009。
简而言之,如果您想在测试期间禁用 Secretsmanager 和参数存储,以下操作是可能的,并且可能会有所帮助:
src/main/test/resources/application.properties spring.cloud.aws.secretsmanager.enabled=false spring.cloud.aws.parameterstore.enabled=false 上面假设使用 src/main/resources/application.yml (具有多文档支持和配置文件激活 - 尽管不是必需的)
如果我在本地配置文件中添加一个区域,它就会起作用:
spring.cloud.aws.region.static=eu-west-1
但是,一旦我不在本地环境中使用 AWS 机密管理,它就不需要区域。
公共类AccountController:控制器 { 私有只读 IAccountService _accountService;
public AccountController(IAccountService accountService)
{
_accountService = accountService;
}
[HttpGet]
public IActionResult Login()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Login(LoginDto loginDto)
{
if(!ModelState.IsValid)
{
return View(loginDto);
}
try
{
await _accountService.LoginAsync(loginDto);
return RedirectToAction("Index", "Home");
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
[HttpGet]
public IActionResult Register()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Register(RegisterDto registerDto)
{
if (!ModelState.IsValid)
{
return View(registerDto);
}
try
{
await _accountService.RegisterAsync(registerDto);
return RedirectToAction("Index", "Home");
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
[HttpGet]
public async Task<IActionResult> AddRoles()
{
try
{
await _accountService.AddRolesAsync();
return RedirectToAction("Index", "Home");
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
[HttpGet]
public async Task<IActionResult> AddAdmin()
{
try
{
await _accountService.AddAdminAsync();
return RedirectToAction("Index", "Home");
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
}