application.yml配置文件:
jwt: userPrimaryKey: userId expireTokenKeyPrefix: jwt:token expireTime: 60000 refreshTokenExpire: 604800000工具类
@Slf4j @Component public class JWTUtil { /** * 读取配置文件到常量中 */ public static JWTConfigProperties gogalConfig; public static String userPrimaryKey; public static Long expireTime; public static Long refreshTokenExpire; @Autowired private JWTConfigProperties jwtConfigProperties; /** * 静态方法想使要使用一个非静态对象,需要做一个初始化【重要】 */ @PostConstruct public void init() { gogalConfig = jwtConfigProperties; userPrimaryKey = jwtConfigProperties.getUserPrimaryKey(); expireTime = jwtConfigProperties.getExpireTime(); refreshTokenExpire = jwtConfigProperties.getRefreshTokenExpire(); } }注入容器类
@Component @ConfigurationProperties(prefix = "jwt") @Data public class JWTConfigProperties { private String userPrimaryKey; private String expireTokenKeyPrefix; /** * jwt过期时间,默认2小时,单位为毫秒 */ private Long expireTime = 7200000L; /** * <pre> * refresh_token过期时间,默认7天,单位为毫秒 * * 常用时间: * 1 day:86400000 * 7 day:604800000 * 14 day:1209600000 * 30 day:2592000000 * </pre> */ private Long refreshTokenExpire = 604800000L; }