先看下文件位置:
文件内容如下:
jwt:
# 加密私钥
secret: bensu[sentiment]
# header名称
header: token
# 有效时长7天(7*24*60*60)
expire: 604800
项目依赖关系如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
</dependency>
<!-- SpringBoot Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--JSP依赖-->
<!--dependency>-->
<!--<groupId>org.apache.tomcat.embed</groupId>-->
<!--<artifactId>tomcat-embed-jasper</artifactId>-->
<!--</dependency>-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 数据库访问控制依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.webjars.bower/jquery -->
<dependency>
<groupId>org.webjars.bower</groupId>
<artifactId>jquery</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.fusesource</groupId>
<artifactId>sigar</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
绑定实体类代码如下:
@Component
@PropertySource("classpath:personal.yml")
@ConfigurationProperties(prefix = "jwt")
public class TokenDomain {
// header名称
@Value(value = "${header}")
private String header;
// 密钥
@Value(value = "${secret}")
private String secret;
// 有效时长
@Value(value = "${expire}")
private long expire;
public String getHeader() {
return header;
}
public void setHeader(String header) {
this.header = header;
}
public String getSecret() {
return secret;
}
public void setSecret(String secret) {
this.secret = secret;
}
public long getExpire() {
return expire;
}
public void setExpire(long expire) {
this.expire = expire;
}
}
调用该实体类代码如下:
@Autowired
private TokenDomain tokenDomain;
/**
* 根据身份ID标识,生成Token
* @param userId 根据身份ID标识
* @param pwd 密码
* @return Token 字串
*/
public String createToken(String userId,String pwd){
//过期时间,精确到毫秒
Date expireDate = new Date(new Date().getTime() + tokenDomain.getExpire());
// 生成token字串
String token =Jwts.builder()
.setHeaderParam("typ","JWT")
.setHeaderParam("alg","HS256")
.claim("userId",userId)
.claim("pwd",pwd)
.setSubject(userId+pwd)
.setIssuedAt(new Date())
.setExpiration(expireDate)
.signWith(SignatureAlgorithm.ES512, tokenDomain.getSecret())
.compact();
return token;
}
实体类在Spring测试类中可正常读取:
@Autowired
TokenDomain token;
@Test
void getPersonal(){
System.out.println(token);
System.out.println(token.getHeader());
System.out.println(token.getSecret());
System.out.println(token.getExpire());
}
测试类返回结果如下:
但是,业务逻辑类一直都无法正常读取内容,domain一直为null
请高手指点迷津。