| | |
| | | package com.doumee.config.Jwt; |
| | | |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| | | import com.doumee.core.constants.ResponseStatus; |
| | | import com.doumee.core.exception.BusinessException; |
| | | import com.doumee.dao.business.model.Member; |
| | | import com.doumee.service.business.impl.MemberServiceImpl; |
| | | import io.jsonwebtoken.*; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | |
| | | import java.util.Date; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.UUID; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | /** |
| | | * <p>后台系统jwt token工具类</p> |
| | |
| | | public static final String HEADER_KEY = "token"; |
| | | //取值名称 |
| | | public static final String UserId_Name = "AppUserId"; |
| | | //取值名称 |
| | | public static final String ShopId_Name = "AppShopId"; |
| | | //加密密钥 |
| | | private final static String jwtSecret = "MhAjU9poLf8ko54K25XBDtonaL33vtt1"; |
| | | //过期时间(s) 86400L=1天 604800L=7天 |
| | | private static final long expire = 86400L; |
| | | |
| | | //redis过期时间 |
| | | private static final Integer redisExpire = 365; |
| | | /** |
| | | * 生成token,根据userId和默认过期时间 |
| | | */ |
| | |
| | | final Date expirationDate = new Date(System.currentTimeMillis() + expiredSeconds * 1000); |
| | | return generateToken(jwtPayLoad.getMemberId(), expirationDate, jwtPayLoad.toMap()); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 生成token,根据userId和默认过期时间 |
| | | */ |
| | | public static String generateTokenForRedis(Integer userId, Integer userType, String userInfo, RedisTemplate<String,Object> redisTemplate) { |
| | | String tokenKey = userType +""+UUID.randomUUID() + "_" + userId; |
| | | redisTemplate.opsForValue().set(tokenKey,userInfo,redisExpire, TimeUnit.DAYS); |
| | | return tokenKey; |
| | | } |
| | | |
| | | public void loginOut(RedisTemplate<String,Object> redisTemplate,String token){ |
| | | redisTemplate.delete(token); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | /** |
| | | * 验证token是否失效 |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 生成token,根据userId和默认过期时间 |
| | | */ |
| | | public static String generateTokenOld(JwtPayLoad jwtPayLoad) { |
| | | Long expiredSeconds = getExpireSeconds(); |
| | | final Date expirationDate = new Date(System.currentTimeMillis() + expiredSeconds * 1000); |
| | | return generateToken(jwtPayLoad.getMemberId(), expirationDate, jwtPayLoad.toMap()); |
| | | } |
| | | |
| | | /** |
| | | * 获取jwt的payload部分 |
| | |
| | | /** |
| | | * 生成token,根据userId和过期时间 |
| | | */ |
| | | public static String generateToken(Integer userId, Date exppiredDate, Map<String, Object> claims) { |
| | | public static String generateToken(String userId, Date exppiredDate, Map<String, Object> claims) { |
| | | |
| | | final Date createdDate = new Date(); |
| | | String secret = getJwtSecret(); |
| | | |
| | | if (claims == null) { |
| | | return Jwts.builder() |
| | | .setSubject(userId.toString()) |
| | | .setSubject(userId) |
| | | .setIssuedAt(createdDate) |
| | | .setExpiration(exppiredDate) |
| | | .signWith(SignatureAlgorithm.HS512, secret) |
| | |
| | | } else { |
| | | return Jwts.builder() |
| | | .setClaims(claims) |
| | | .setSubject(userId.toString()) |
| | | .setSubject(userId) |
| | | .setIssuedAt(createdDate) |
| | | .setExpiration(exppiredDate) |
| | | .signWith(SignatureAlgorithm.HS512, secret) |