<dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifactId> <version>0.9.1</version> </dependency>
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.57</version> </dependency>
import com.alibaba.fastjson.JSON; import com.lemo.saas.constant.ExceptionEnum; import com.lemo.saas.constant.JwtConstants; import com.lemo.saas.exception.MyException; import io.jsonwebtoken.Claims; import io.jsonwebtoken.ExpiredJwtException; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import javax.servlet.http.HttpServletRequest; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * token工具类 * * @author zhangbin 2019年05月22日 * @version 1.0 * @since 1.0 2019年05月22日 */ @Component public class JwtUtils { @Autowired private HttpServletRequest request; @Autowired private StringUtil stringUtil; // @Autowired // private StringRedisTemplate redisTemplate; /** * @return java.lang.String * @author zhangBin * @descriptions //TODO 获取toke * @date 2019/5/22 9:05 **/ public String getToken() { String token = request.getHeader(JwtConstants.AUTH_HEADER); if (StringUtils.isEmpty(token) || !token.startsWith("Bearer ")) { throw new MyException(ExceptionEnum.TOKEN_INVALID_ERROR); } token = token.substring(7); return token; } /** * @return java.lang.String * @author zhangBin * @descriptions //TODO 获取用户名 * @date 2019/5/16 16:32 **/ public String getUserNameFromToken() { String subject = getClaimsFromToken(getToken()).getSubject(); return subject; } /** * @return java.util.Map * @author zhangBin * @descriptions //TODO 将token对象信息转换成map对象 * @date 2019/5/16 16:45 **/ public Map getUserInfoFromToken() { return JSON.parseObject(getClaimsFromToken(getToken()).getSubject(), Map.class); } /** * @param param 需要获取的参数 * @return java.lang.String * @author zhangBin * @descriptions //TODO 获取用户对象的信息 * @date 2019/5/16 16:52 **/ public String getParamsToken(String param) { Map userInfoFromToken = getUserInfoFromToken(); return stringUtil.replaceBlank(userInfoFromToken.get(param) + "\n"); } /** * @param token token参数 * @return io.jsonwebtoken.Claims * @author zhangBin * @descriptions //TODO 解析token,获取jwt的playLoad部分 * @date 2019/5/16 16:30 **/ public Claims getClaimsFromToken(String token) { Claims body; try { body = Jwts.parser() .setSigningKey(JwtConstants.SECRET) .parseClaimsJws(token) .getBody(); } catch (Exception e) { throw new MyException(ExceptionEnum.TOKEN_INVALID_ERROR); } return body; } /** * <pre> * 验证token是否失效 * true:过期 false:没过期 * </pre> */ public Boolean isTokenExpired() { try { final Date expiration = getExpirationDateFromToken(); return expiration.before(new Date()); } catch (ExpiredJwtException expiredJwtException) { return true; } } /** * 获取jwt失效时间 */ public Date getExpirationDateFromToken() { return getClaimsFromToken(getToken()).getExpiration(); } /** * 生成token(通过用户名和签名时候用的随机数) */ /*public static String generateToken(String userId) { Map<String, Object> claims = new HashMap<>(); return doGenerateToken(claims, userId); }*/ public String generateToken(String userInfoInToken) { Map<String, Object> claims = new HashMap<>(); return doGenerateToken(claims, userInfoInToken); } /** * 生成token */ private String doGenerateToken(Map<String, Object> claims, String subject) { final Date createdDate = new Date(); final Date expirationDate = new Date(createdDate.getTime() + JwtConstants.EXPIRATION * 1000); return Jwts.builder() .setClaims(claims) .setSubject(subject) .setIssuedAt(createdDate) .setExpiration(expirationDate) .signWith(SignatureAlgorithm.HS512, JwtConstants.SECRET) .compact(); } /** * 获取混淆MD5签名用的随机字符串 */ public String getRandomKey() { return stringUtil.getRandomString(6); } }