¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.yuanchu.limslaboratory.shiro.utils; |
| | | |
| | | |
| | | import com.auth0.jwt.JWT; |
| | | import com.auth0.jwt.JWTCreator; |
| | | import com.auth0.jwt.JWTVerifier; |
| | | import com.auth0.jwt.algorithms.Algorithm; |
| | | import com.auth0.jwt.exceptions.JWTCreationException; |
| | | import com.auth0.jwt.exceptions.JWTDecodeException; |
| | | import com.auth0.jwt.exceptions.JWTVerificationException; |
| | | import com.auth0.jwt.interfaces.Claim; |
| | | import com.auth0.jwt.interfaces.DecodedJWT; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import java.util.Calendar; |
| | | import java.util.Date; |
| | | import java.util.Map; |
| | | |
| | | @Component |
| | | public class JwtUtils { |
| | | private static String staticSecret; |
| | | |
| | | @Value("${login.secret}") |
| | | private String secret; |
| | | |
| | | @PostConstruct |
| | | public void getApiToken() { |
| | | staticSecret = this.secret; |
| | | } |
| | | |
| | | public static String getSecret() { |
| | | // lockie.zou |
| | | return staticSecret; |
| | | } |
| | | |
| | | // è¿ææ¶é´ 2 å°æ¶ |
| | | private static final long EXPIRE_TIME = 2 * 60 * 60 * 1000; |
| | | //èªå·±å®å¶å¯é¥ |
| | | public static final String SECRET = "J-(t]Poe9P"; |
| | | |
| | | //请æ±å¤´ |
| | | public static final String AUTH_HEADER = "X-Token"; // X-Authorization-With |
| | | |
| | | /** |
| | | * éªè¯tokenæ¯å¦æ£ç¡® |
| | | * @param token |
| | | * @return |
| | | */ |
| | | public static boolean verify(String token){ |
| | | try{ |
| | | String account = getClaimFiled(token, "account"); |
| | | if (account == null){ |
| | | return false; |
| | | } |
| | | Algorithm algorithm = Algorithm.HMAC256(getSecret()); |
| | | JWTVerifier verifier = JWT.require(algorithm).withClaim("account",account).build(); |
| | | verifier.verify(token); |
| | | return true; |
| | | } catch (JWTVerificationException exception){ |
| | | return false; |
| | | } catch (Exception e){ |
| | | e.printStackTrace(); |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·å¾tokenä¸çèªå®ä¹ä¿¡æ¯,ä¸è¬æ¯è·åtokençusernameï¼æ ésecretè§£å¯ä¹è½è·å¾ |
| | | * @param token |
| | | * @param filed |
| | | * @return |
| | | */ |
| | | public static String getClaimFiled(String token, String filed){ |
| | | try{ |
| | | if (!ObjectUtils.isEmpty(token)){ |
| | | DecodedJWT jwt = JWT.decode(token); |
| | | return jwt.getClaim(filed).asString(); |
| | | } |
| | | return null; |
| | | } catch (JWTDecodeException e){ |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * çæç¾å,åç¡®å°è¯´æ¯çætoken |
| | | * @return |
| | | */ |
| | | public static String sign(String account){ |
| | | try{ |
| | | Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME); |
| | | Algorithm algorithm = Algorithm.HMAC256(SECRET); |
| | | //é带username,nicknameä¿¡æ¯ |
| | | return JWT.create() |
| | | .withClaim("account",account) |
| | | .withExpiresAt(date) |
| | | .sign(algorithm); |
| | | } catch (JWTCreationException e){ |
| | | e.printStackTrace(); |
| | | return null; |
| | | } catch (Exception e){ |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·åtokençç¾åæ¶é´ |
| | | * @param token |
| | | * @return |
| | | */ |
| | | public static Date getIssueAt(String token){ |
| | | try{ |
| | | DecodedJWT jwt = JWT.decode(token); |
| | | return jwt.getIssuedAt(); |
| | | } catch (JWTDecodeException e){ |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * éªè¯tokenæ¯å¦è¿æ |
| | | * @param token |
| | | * @return |
| | | */ |
| | | public static boolean isTokenExpired(String token){ |
| | | Date now = Calendar.getInstance().getTime(); |
| | | DecodedJWT jwt = JWT.decode(token); |
| | | return jwt.getExpiresAt().before(now); |
| | | } |
| | | |
| | | /** |
| | | * å·æ°tokençæææ |
| | | * @param token |
| | | * @param secret |
| | | * @return |
| | | */ |
| | | public static String refreshTokenExpired(String token, String secret){ |
| | | DecodedJWT jwt = JWT.decode(token); //è§£ætoken |
| | | Map<String, Claim> claims = jwt.getClaims(); //è·åtokençåæ°ä¿¡æ¯ |
| | | |
| | | try{ |
| | | Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME); |
| | | Algorithm algorithm = Algorithm.HMAC256(secret); |
| | | JWTCreator.Builder builder = JWT.create().withExpiresAt(date); |
| | | for(Map.Entry<String,Claim> entry : claims.entrySet()){ |
| | | builder.withClaim(entry.getKey(),entry.getValue().asString()); |
| | | } |
| | | return builder.sign(algorithm); |
| | | } catch (JWTCreationException e){ |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | } |
| | | } |