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

Spring Boot启动报错:SecurityConfig构造器缺失RsaKeyProperties Bean

问题解决:Spring Boot中RsaKeyProperties Bean未找到报错

问题场景

跟随Spring Boot Demo编写代码,仅在securityFilterChain方法添加@SuppressWarnings("removal")注解,启动应用时触发报错:

Parameter 0 of constructor in com.example.SpringRestDemo.security.SecurityConfig required a bean of type 'com.example.SpringRestDemo.config.RsaKeyProperties' that could not be found.

原因分析

RsaKeyProperties类仅添加了@ConfigurationProperties(prefix = "rsa")注解,该注解仅用于绑定配置文件属性,不会自动将类注册为Spring容器中的Bean,导致SecurityConfig构造器依赖注入时找不到对应实例。

解决方法

两种可行方案,任选其一即可:

方案一:给RsaKeyProperties添加@Component注解

直接在RsaKeyProperties类上添加@Component,让Spring自动扫描并注册为Bean:

package com.example.SpringRestDemo.config;

import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "rsa")
public record RsaKeyProperties(RSAPublicKey publicKey, RSAPrivateKey privateKey) {

}

方案二:在主启动类添加@EnableConfigurationProperties注解

在项目主启动类(SpringRestDemoApplication)上添加@EnableConfigurationProperties(RsaKeyProperties.class),显式启用该配置属性类的Bean注册:

package com.example.SpringRestDemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import com.example.SpringRestDemo.config.RsaKeyProperties;

@SpringBootApplication
@EnableConfigurationProperties(RsaKeyProperties.class)
public class SpringRestDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringRestDemoApplication.class, args);
    }
}

额外注意事项

确保application.properties或application.yml中配置了RSA密钥相关属性,否则即使Bean注册成功,也会出现密钥绑定失败的错误:

application.yml示例

rsa:
  public-key: "你的公钥内容"
  private-key: "你的私钥内容"

application.properties示例

rsa.public-key=你的公钥内容
rsa.private-key=你的私钥内容

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 01:23:17