属性 源文件的内容在某些 类 中无法识别
Contents of property source files are not recognized in some classes
当从特定 class.
调用时,@Value 注释不会从属性文件中获取 属性
属性配置:
@Configuration
@PropertySource(value = "classpath:s_app.properties")
@PropertySource(value = "classpath:sDBpools.properties")
public class PropertiesConfig {
}
当我从 ProjectInfoServiceImpl class 调用 属性 时,它 returns 但在 JwtTokenUtil class.
中不起作用
秘密 returns 此处为空:
@Component
@Log4j2
public class JwtTokenUtil implements Serializable {
private static final long serialVersionUID = -1L;
private static final int EXPIRATION_MILLISECONDS = 60 * 60 * 1000;
@Value("${info.app.jwt.secret}")
private String secret;
public String generateToken(User user){
Map<String, Object> claims = new HashMap<>();
claims.put("authorities", user.getAuthorities());
return generateToken(user.getUsername(), claims);
}
public String generateToken(UserDetails userDetails){
Map<String, Object> claims = new HashMap<>();
claims.put("authorities", userDetails.getAuthorities());
return generateToken(userDetails.getUsername(), claims);
}
public String generateToken(String username, Map<String, Object> claims) {
return Jwts.builder()
.setSubject(username)
.setClaims(claims)
.setExpiration(new Date((System.currentTimeMillis() + EXPIRATION_MILLISECONDS)))
.setIssuedAt(new Date())
.signWith(SignatureAlgorithm.HS256, secret)
.compact();
}
public boolean validateToken(String username, String token){
return (username.equals(getUsernameFromToken(token)) && !isExpired(token));
}
public String getUsernameFromToken(String token) {
return getClaimFromToken(token, Claims::getSubject);
}
public Date getExpirationdateFromToken(String token){
return getClaimFromToken(token, Claims::getExpiration);
}
public boolean isExpired(String token){
return !(new Date().before(getExpirationdateFromToken(token)));
}
public <T> T getClaimFromToken(String token, Function<Claims, T> claimsResolver) {
final Claims claims = getAllClaimsFromToken(token);
return claimsResolver.apply(claims);
}
private Claims getAllClaimsFromToken(String token) {
return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
}
}
但在这里 returns 正如预期的那样:
@Service
@Log4j2
public class ProjectInfoServiceImpl implements ProjectInfoService {
@Value("${info.app.jwt.secret}")
private String secret;
@Value("${s.version}")
private String version;
@Value("${s.deployment.profile}")
private String deploymentProfile;
@Value("${s.deployment.environment}")
private String deploymentEnvironment;
@Override
public ProjectInfoDTO getProjectInfo() {
ProjectInfoDTO projectInfoDTO = new ProjectInfoDTO();
projectInfoDTO.setVersion(version);
projectInfoDTO.setDeploymentProfile(deploymentProfile);
projectInfoDTO.setDeploymentEnvironment(deploymentEnvironment);
return projectInfoDTO;
}
}
文件夹结构如下:
此外,执行器无法从 s_properties 文件的 info.app.version 属性 中获取信息。 /info endpoint returns 没有信息,因为它找不到 属性.
问题已通过以下问题的批准答案解决。
我手动实例化了 JwtTokenUtil class 而不是在调用者 class 中自动装配它。可能我根本没有到达代理对象。
当从特定 class.
调用时,@Value 注释不会从属性文件中获取 属性属性配置:
@Configuration
@PropertySource(value = "classpath:s_app.properties")
@PropertySource(value = "classpath:sDBpools.properties")
public class PropertiesConfig {
}
当我从 ProjectInfoServiceImpl class 调用 属性 时,它 returns 但在 JwtTokenUtil class.
中不起作用秘密 returns 此处为空:
@Component
@Log4j2
public class JwtTokenUtil implements Serializable {
private static final long serialVersionUID = -1L;
private static final int EXPIRATION_MILLISECONDS = 60 * 60 * 1000;
@Value("${info.app.jwt.secret}")
private String secret;
public String generateToken(User user){
Map<String, Object> claims = new HashMap<>();
claims.put("authorities", user.getAuthorities());
return generateToken(user.getUsername(), claims);
}
public String generateToken(UserDetails userDetails){
Map<String, Object> claims = new HashMap<>();
claims.put("authorities", userDetails.getAuthorities());
return generateToken(userDetails.getUsername(), claims);
}
public String generateToken(String username, Map<String, Object> claims) {
return Jwts.builder()
.setSubject(username)
.setClaims(claims)
.setExpiration(new Date((System.currentTimeMillis() + EXPIRATION_MILLISECONDS)))
.setIssuedAt(new Date())
.signWith(SignatureAlgorithm.HS256, secret)
.compact();
}
public boolean validateToken(String username, String token){
return (username.equals(getUsernameFromToken(token)) && !isExpired(token));
}
public String getUsernameFromToken(String token) {
return getClaimFromToken(token, Claims::getSubject);
}
public Date getExpirationdateFromToken(String token){
return getClaimFromToken(token, Claims::getExpiration);
}
public boolean isExpired(String token){
return !(new Date().before(getExpirationdateFromToken(token)));
}
public <T> T getClaimFromToken(String token, Function<Claims, T> claimsResolver) {
final Claims claims = getAllClaimsFromToken(token);
return claimsResolver.apply(claims);
}
private Claims getAllClaimsFromToken(String token) {
return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
}
}
但在这里 returns 正如预期的那样:
@Service
@Log4j2
public class ProjectInfoServiceImpl implements ProjectInfoService {
@Value("${info.app.jwt.secret}")
private String secret;
@Value("${s.version}")
private String version;
@Value("${s.deployment.profile}")
private String deploymentProfile;
@Value("${s.deployment.environment}")
private String deploymentEnvironment;
@Override
public ProjectInfoDTO getProjectInfo() {
ProjectInfoDTO projectInfoDTO = new ProjectInfoDTO();
projectInfoDTO.setVersion(version);
projectInfoDTO.setDeploymentProfile(deploymentProfile);
projectInfoDTO.setDeploymentEnvironment(deploymentEnvironment);
return projectInfoDTO;
}
}
文件夹结构如下:
此外,执行器无法从 s_properties 文件的 info.app.version 属性 中获取信息。 /info endpoint returns 没有信息,因为它找不到 属性.
问题已通过以下问题的批准答案解决。 我手动实例化了 JwtTokenUtil class 而不是在调用者 class 中自动装配它。可能我根本没有到达代理对象。