Cannot invoke “String.getBytes(java.nio.charset.Charset)“ because “secretKey“ is null

错误java.lang.NullPointerException: Cannot invoke "String.getBytes(java.nio.charset.Charset)" because "secretKey" is null

在做一个微服务项目时,我在common模块定义了Jwt配置类,用于从配置文件application.yml中读取Jwt配置(自定义密钥、过期时间等),遇到了上述错误。

/**
 * @author
 *  Jwt配置读取类
 */
@Component
@ConfigurationProperties(prefix = "my.jwt")
@Data
public class JwtProperties {
    // 密钥
    private String secretKey;

    // 过期时间
    private long expireTime;

//    // 保存token的字段名称
//    private String tokenHead;
}

原因:排查发现是因为我需要的是在user子模块实现生成jwt的功能,但是我将jwt的配置信息写在了common模块的配置文件中

解决办法:正确的做法是在user模块的application.yml中配置jwt配置信息

my:
  jwt:
    secret-key: ***** # 密钥
    expire-time: 36000000 # 过期时间(单位:毫秒)