/** * Copyright (c) 2018 人人开源 All rights reserved. * * https://www.renren.io * * 版权所有,侵权必究! */ package com.epmet.jwt; import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; import org.joda.time.DateTime; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Date; /** * Jwt工具类 * * @author Mark sunlightcs@gmail.com * @since 1.0.0 */ @Component public class JwtUtils { private static final Logger logger = LoggerFactory.getLogger(JwtUtils.class); /* @Autowired private JwtProperties jwtProperties;*/ @Autowired private JwtTokenProperties jwtProperties; /** * 生成jwt token */ public String generateToken(Long userId) { return Jwts.builder() .setHeaderParam("typ", "JWT") .setSubject(userId+"") .setIssuedAt(new Date()) .setExpiration(DateTime.now().plusSeconds(jwtProperties.getExpire()).toDate()) .signWith(SignatureAlgorithm.HS512, jwtProperties.getSecret()) .compact(); } public Claims getClaimByToken(String token) { try { return Jwts.parser() .setSigningKey(jwtProperties.getSecret()) .parseClaimsJws(token) .getBody(); }catch (Exception e){ logger.debug("validate is token error, token = " + token, e); return null; } } /** * token是否过期 * @return true:过期 */ public boolean isTokenExpired(Date expiration) { return expiration.before(new Date()); } }