英文:
Java convert class from Java 14 to 1.7
问题
以下是将Java 14中的代码转换为Java 1.7的部分:
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* 创建Jwt令牌及其他实用方法。
*/
public class JwtTokenUtil {
private String SECRET_KEY = "secret-876123";
public String extractUserName(String token) {
return extractClaim(token, new Function<Claims, String>() {
@Override
public String apply(Claims claims) {
return claims.getSubject();
}
});
}
public Date extractExpiration(String token) {
return extractClaim(token, new Function<Claims, Date>() {
@Override
public Date apply(Claims claims) {
return claims.getExpiration();
}
});
}
public <T> T extractClaim(String token, Function<Claims, T> claimsResolver) {
final Claims claims = extractAllClaims(token);
return claimsResolver.apply(claims);
}
private Claims extractAllClaims(String token) {
return Jwts.parser().setSigningKey(SECRET_KEY).parseClaimsJws(token).getBody();
}
private Boolean isTokenExpired(String token) {
return extractExpiration(token).before(new Date());
}
/**
* 传入用户详细信息,生成Jwt令牌
* @param userDetails
* @return
*/
public String generateToken(UserDetails userDetails) {
Map<String,Object> claims = new HashMap<>();
return createToken(claims, userDetails.getUsername());
}
private String createToken(Map<String,Object> claims, String subject) {
return Jwts.builder().setClaims(claims).setSubject(subject).setIssuedAt(new Date(System.currentTimeMillis()))
.setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 10)) // 10小时
.signWith(SignatureAlgorithm.HS256, SECRET_KEY).compact();
}
public Boolean validteToken(String token, UserDetails userDetails) {
final String username = extractUserName(token);
return (username.equals(userDetails.getUsername()) && !isTokenExpired(token));
}
}
主要更改包括使用匿名内部类替代Lambda表达式,并删除了不支持的java.util.function.Function
导入。希望这可以帮助您解决编译错误。
英文:
I know usually people do this the other way around, but how do I convert the following class to from Java 14 to Java 1.7.
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
/**
* Creates Jwt token and other util methods.
*/
public class JwtTokenUtil {
private String SECRET_KEY = "secret-876123";
public String extractUserName(String token) {
return extractClaim(token, Claims::getSubject);
}
public Date extractExpiration(String token) {
return extractClaim(token, Claims::getExpiration);
}
public <T> T extractClaim(String token, Function<Claims, T> claimsResolver) {
final Claims claims = extractAllClaims(token);
return claimsResolver.apply(claims);
}
private Claims extractAllClaims(String token) {
return Jwts.parser().setSigningKey(SECRET_KEY).parseClaimsJws(token).getBody();
}
private Boolean isTokenExpired(String token) {
return extractExpiration(token).before(new Date());
}
/**
* Pass in user details, and get a Jwt
* @param userDetails
* @return
*/
public String generateToken(UserDetails userDetails) {
Map<String,Object> claims = new HashMap<>();
return createToken(claims, userDetails.getUsername());
}
private String createToken(Map<String,Object> claims, String subject) {
return Jwts.builder().setClaims(claims).setSubject(subject).setIssuedAt(new Date(System.currentTimeMillis()))
.setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 10)) // 10 hrs
.signWith(SignatureAlgorithm.HS256, SECRET_KEY).compact();
}
public Boolean validteToken(String token, UserDetails userDetails) {
final String username = extractUserName(token);
return (username.equals(userDetails.getUsername()) && !isTokenExpired(token));
}
}
There are compilation errors on:
- the lambda expressions:
Claims::getSubject
and
Claims::getExpiration
. - the following import does not exist in Java 1.7:
import
java.util.function.Function;
答案1
得分: 1
你可以编写自己的 Function
实现,并用新创建的 Function
接口的实现替换方法引用,但我猜你需要转换的不仅仅是一个类,所以我建议使用 Retrolambda。
英文:
You could write your own implementation of Function
and replace the method references with implementations of newly created Function
interface, but I'm guessing you have to convert much more than a single class, so I recommend Retrolambda.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论