Spring Boot 找不到 Bean

发布于 2025-01-10 20:36:04 字数 1735 浏览 0 评论 0原文

我在尝试使用标记化启动 Spring Boot 应用程序时遇到问题。这是我的服务类:

@Service
@Slf4j
public class JwtTokenService {
    private final JwtConfig jwtConfig;

    public JwtTokenService(JwtConfig jwtConfig) {
        this.jwtConfig = jwtConfig;
    }

    public String generateToken(Authentication authentication) {
        Long now = System.currentTimeMillis();
        return Jwts.builder()
                .setSubject(authentication.getName())
                .claim("authorities", authentication.getAuthorities().stream()
                        .map(GrantedAuthority::getAuthority).collect(Collectors.toList()))
                .setIssuedAt(new Date(now))
                .setExpiration(new Date(now + jwtConfig.getExpiration() * 1000))
                .signWith(SignatureAlgorithm.HS512, jwtConfig.getSecret().getBytes())
                .compact();
    }

}

这是我的配置类:

@Data
@NoArgsConstructor
@Component
public class JwtConfig {
    @Value("${security.jwt.uri:/auth/**}")
    private String Uri;

    @Value("${security.jwt.header:Authorization}")
    private String header;

    @Value("${security.jwt.prefix:Bearer }")
    private String prefix;

    @Value("${security.jwt.expiration:#{24*60*60}}")
    private int expiration;

    @Value("${security.jwt.secret:JwtSecretKey}")
    private String secret;
}

当我尝试运行我的应用程序时,出现以下错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in services.JwtTokenService required a bean of type 'config.JwtConfig' that could not be found.


Action:

Consider defining a bean of type 'config.JwtConfig' in your configuration.

我不明白为什么会出现此错误。

I have a problem when trying to start my Spring Boot application with tokenization. This is my service class:

@Service
@Slf4j
public class JwtTokenService {
    private final JwtConfig jwtConfig;

    public JwtTokenService(JwtConfig jwtConfig) {
        this.jwtConfig = jwtConfig;
    }

    public String generateToken(Authentication authentication) {
        Long now = System.currentTimeMillis();
        return Jwts.builder()
                .setSubject(authentication.getName())
                .claim("authorities", authentication.getAuthorities().stream()
                        .map(GrantedAuthority::getAuthority).collect(Collectors.toList()))
                .setIssuedAt(new Date(now))
                .setExpiration(new Date(now + jwtConfig.getExpiration() * 1000))
                .signWith(SignatureAlgorithm.HS512, jwtConfig.getSecret().getBytes())
                .compact();
    }

}

This is my config class:

@Data
@NoArgsConstructor
@Component
public class JwtConfig {
    @Value("${security.jwt.uri:/auth/**}")
    private String Uri;

    @Value("${security.jwt.header:Authorization}")
    private String header;

    @Value("${security.jwt.prefix:Bearer }")
    private String prefix;

    @Value("${security.jwt.expiration:#{24*60*60}}")
    private int expiration;

    @Value("${security.jwt.secret:JwtSecretKey}")
    private String secret;
}

And I get the following error when I try to run my Application:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in services.JwtTokenService required a bean of type 'config.JwtConfig' that could not be found.


Action:

Consider defining a bean of type 'config.JwtConfig' in your configuration.

I dont understand why i get this error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执着的年纪 2025-01-17 20:36:04

我解决了。问题是我的 Spring Boot 应用程序没有扫描配置所在的包。在我的 @SpringBootApplication 中,我添加了 @ComponentScan 和 JwtConfig 所在的包。

I solved it. The problem was that the package where the config lays was not scanned by my spring boot application. In my @SpringBootApplication I added @ComponentScan with the package where JwtConfig lays.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文