Java将类从Java 14转换为1.7

huangapple go评论150阅读模式
英文:

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 = &quot;secret-876123&quot;;
public String extractUserName(String token) {
return extractClaim(token, Claims::getSubject);
}
public Date extractExpiration(String token) {
return extractClaim(token, Claims::getExpiration);
}
public &lt;T&gt; T extractClaim(String token, Function&lt;Claims, T&gt; 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&lt;String,Object&gt; claims = new HashMap&lt;&gt;();
return createToken(claims, userDetails.getUsername());
}
private String createToken(Map&lt;String,Object&gt; 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()) &amp;&amp; !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.

huangapple
  • 本文由 发表于 2020年8月4日 14:58:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63241667.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定