[英]Bean that could not be found Spring Boot
嘗試使用令牌化啟動我的 Spring 啟動應用程序時遇到問題。 這是我的服務 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();
}
}
這是我的配置 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;
}
當我嘗試運行我的應用程序時出現以下錯誤:
***************************
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.
我不明白為什么會出現此錯誤。
我解決了。 問題是配置所在的 package 沒有被我的 spring 啟動應用程序掃描。 在我的@SpringBootApplication 中,我在 JwtConfig 所在的位置添加了帶有 package 的 @ComponentScan。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.